Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a LIKE considering two columns?

I have a customer table with two columns first_name and last_name.

How can I use LIKE in a query being able to get data from both columns at same tame?

For instance:

SELECT CONCAT(first_name, ' ', last_name) as 'full_name' 
FROM customer WHERE full_name LIKE 'John D%'

I've tried this and it tells me full_name column doesn't exist.

like image 853
RedDragon Avatar asked Aug 11 '11 14:08

RedDragon


2 Answers

SELECT CONCAT(first_name, ' ', last_name) as 'full_name' 
FROM customer WHERE CONCAT(first_name, ' ', last_name) LIKE 'John D%'
like image 134
Ned Batchelder Avatar answered Sep 20 '22 13:09

Ned Batchelder


You are almost there

SELECT * 
FROM customer 
WHERE CONCAT(first_name, ' ', last_name) LIKE 'John D%'

Note: this may not have very good performance. You might want to consider full text search.

like image 38
Nivas Avatar answered Sep 22 '22 13:09

Nivas