MySQL - CONCAT two fields and use them in WHERE clause

14,355

Try this ::

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

Related videos on Youtube

yoda
Author by

yoda

Freelance Web Developer

Updated on October 16, 2022

Comments

  • yoda
    yoda over 1 year

    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!

  • yoda
    yoda almost 12 years
    Thanks, that did the trick. I was actually expecting some way to use the aliased field in where clause, don't know if it's possible.