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!
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.
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.
Try this ::
SELECT CONCAT_WS(' ', first_name, last_name) AS name FROM `users`
WHERE CONCAT_WS(' ', first_name, last_name) LIKE "%John Doe%"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With