Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - CONCAT two fields and use them in WHERE clause

As the title suggests, I was wondering how to concat two fields in a where clause in mysql. This is an example of what I'm trying to achieve :

SELECT CONCAT_WS(' ', first_name, last_name) AS name FROM `users`
WHERE name LIKE "%John Doe%"

The point is that first_name and last_name are separate fields, and I want to enable my PHP application to search for a full person's name.

Any tips?

Cheers!

like image 826
yoda Avatar asked Jun 21 '12 14:06

yoda


People also ask

How do I concatenate two fields in SQL?

Query: SELECT *, CONCAT(FIRSTNAME, LASTNAME) AS FIRSTNAME FROM demo_table; Output: Here, we can see that FIRSTNAME and LASTNAME is concatenated but there is no space between them, If you want to add space between the FIRSTNAME and LASTNAME then add space(' ') in CONCAT() function.

How do I concatenate two columns in SQL with spaces?

To concatenate two string type columns separated by space, we can use space function. Notice the SPACE(1) function in between FirstName and LastName. As the parameter value passed in SPACE function is 1 so there will be one blank space in between FirstName and LastName column values.


1 Answers

Try this ::

SELECT CONCAT_WS(' ', first_name, last_name) AS name FROM `users`
WHERE CONCAT_WS(' ', first_name, last_name) LIKE "%John Doe%"
like image 108
Sashi Kant Avatar answered Nov 09 '22 10:11

Sashi Kant