How to use ILIKE in postgres with two columns - sql

I have two columns which are called surname and name.
For example I have 3 users
Zuckerberg Mark
Gates Bill
Jobs Steve
If my input is only one phrase such as "Ma" or "Mark" or "Gate" etc. my query works well, but if i enter and surname and name I got an empty result
select
u.surname,
u.name,
u.avatar
from users u
where (surname ilike '%Mark Zuckerberg%' or name ilike '%Mark Zuckerberg%')
order by surname, name
How can I rebuild my query for this goal?

Sorry my previous answer had a typo, but this works:
select
u.surname,
u.name,
u.avatar
from users u
where ((u.surname ||' '|| u.name) ilike '%Mark Zuckerberg%' or (u.name ||' '||u.surname) ilike '%Mark Zuckerberg%')
order by surname, name
Here's a sqlfiddle link: http://sqlfiddle.com/#!17/4e6ce/7

Related

Serach row where first name and last name matches against full name separated by space

I have two columns first_name and last_name.
To search for a row against passed search text I am using iLik query.
SELECT * FROM t1 WHERE t.first_name ILIKE %John% or t.last_name ILIKE %John%;
Above works.
But I want to search for full name, both on first_name and last_name. It does not return the row.
SELECT * FROM t1 WHERE t.first_name ILIKE %John Doe% or t.last_name ILIKE %John Doe%;
Above empty result.
How can I make this to search on both the columns matching against sub parts of the search text?
I presume that John & Doe are in the two separate columns, so 'John' in t.first_name & 'Doe' in t.last_name. Your bottom query is looking for the full name in just one of the columns. If the names have been split out between the two columns then the full name will not appear in one column. I am pressuming that the columns will also just contain that first and last name, so the ILIKE will not be needed and you can just run an equals to,In order to achieve what you need, please run the following:
Equals version - SELECT * FROM t1 WHERE t.first_name = 'John' AND t.last_name = 'Doe';
Wildcard version - SELECT * FROM t1 WHERE t.first_name ILIKE %John% AND t.last_name ILIKE %Doe%;
Let me know how you get on.
I tried using CONCAT function and it's working,
SELECT *
FROM t1
WHERE CONCAT(t.first_name, ' ', t.last_name) ILIKE '%John Doe%';

How to still return psql query if value in row is null (value also linked to other row)

I have two tables in postgres db. Companies and bootcampers. Companies.company_id is primary key to bootcampers.company_id foreign key.
Trying to select all rows from bootcampers and company_id and company_name from companies based on company_id. Some bootcampers will not have a company_id yet. How can i change this psql query to say please return all fields but if company_id is blank, return everything else anyway. Is there a 'null is ok' thing i need to use?
SELECT uid,
email,
photourl,
first_name,
surname,
aboutme,
job_title,
company_name,
salary,
start_date,
previous_roles,
cohort_num,
region,
job_satisfaction,
new_job,
bootcampers.twitter,
github,
portfolio,
bootcampers.linkedin
FROM companies
INNER JOIN bootcampers ON bootcampers.company_id = companies.company_id
WHERE first_name ILIKE '%' || $1 || '%'
Since you want everything from Bootcampers you need a RIGHT JOIN instead of an INNER JOIN
SELECT uid,
email,
photourl,
first_name,
surname,
aboutme,
job_title,
company_name,
salary,
start_date,
previous_roles,
cohort_num,
region,
job_satisfaction,
new_job,
bootcampers.twitter,
github,
portfolio,
bootcampers.linkedin
FROM companies
RIGHT JOIN bootcampers ON bootcampers.company_id = companies.company_id
WHERE first_name ILIKE '%' || $1 || '%'
Be careful if you want to add a filter on a column from the companies table as that could negate your RIGHT JOIN and make it work like an INNER again.

List all actors’ first and last names and their roles which start with the letter ‘P’

List all actors’ first and last names and their roles which start with the letter ‘P’.
I've tried doing this but im not sure exactly why its not working.
SELECT a.first_name, a.last_name, r.role
FROM actors a
WHERE first_name, last_name, role like ‘P’
You would need to join actors and roles.
Assuming structures like:
actors(id, first_name, last_name)
roles(role_id, actor_id, role)
Query:
select a.first_name, a.last_name, r.role
from actors a
inner join roles r on a.id = r.actor_id and r.role like 'P%'
If you have one table with the following structure
---------
actors
---------
id
first_name
last_name
role
---------
you can use the following code
SELECT first_name, last_name, role
FROM actors
WHERE role like "p%"
if you want to use alias then
SELECT a.first_name, a.last_name, a.role
FROM actors AS a
WHERE role like "p%"

Select all rows that matches any from other table

Get all Users from first table that have any word matching in another table;
I have Users table that contain, among others, column with FullName, that column is Full Text Search Indexed. Sometimes first "word" is name, sometimes first "word" is surname from the FullName column;
Like John Smith or Smith John.
And another table that have just local firstname.
I want to get all Users that have matching local name.
Users Table:
John Smith
Rebecca Mark
Maria Anna
Lance Maria
Emilia Clark
Snow John
Natalie Butler
Name Table:
Maria
Smith
Result of Query:
John Smith
Maria Anna
Lance Maria
Snow John
I can do only single Name with Contains function.
SELECT * FROM Users WHERE CONTAINS(FullName, 'John');
But I need it for each row in Name Table.
Every row from FullName contains any of Name Table... But in SQL Query.
To avoid a case where you are search for 'Maria' and the matching name is 'Marianne', check for 2 conditions: (1) the name is at the start or (2) at the end of FullName:
(1):
SELECT u.*
FROM Users u INNER JOIN Name n
ON
u.FullName LIKE concat(n.name, ' %')
OR
u.FullName LIKE concat('% ', n.name)
or (2):
SELECT u.*
FROM Users u INNER JOIN Name n
ON
concat(' ', u.FullName, ' ') LIKE concat('% ', n.name, ' %')
use join and like for matching
select u.* from table_users u join table_name b on
u.users like concat('%',b.name,'%')
You can use exists for this:
select u.*
from users u
where exists (select 1
from nametable n
where u.fullname like '%' + n.name + '%'
);
If you want to avoid partial matches on names, then take the delimiters into account:
where exists (select 1
from nametable n
where ' ' + u.fullname + ' ' like '% ' + n.name + ' %'
);

How to solve union de-duplication problem in SQL

If I want to make a Union between two tables (basketbalplayers and footballplayers), to only select one time the students who play both sports. But the problem is footballplayers use helmets which is a column in footballplayers table but basketball players don't have helmets. What should I do?
You don't have to query all the columns - query just the once you need (read: the columns commons to both tables):
SELECT first_name, last_name
FROM footballplayers
UNION
SELECT first_name, last_name
FROM basketballplayers
I think you said you only want players who play both sports:
select first_name, last_name
from footballplayers f
inner join basketballplayers b on f.first_name = b.firstname and f.last_name = b.last_name