MySQL - CONCAT two fields and use them in the WHERE clause

As the name implies, I was wondering how to concat two fields in 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 fact is that first_name and last_name are separate fields, and I want to include a PHP application to search for a person’s full name.

Any tips?

Hurrah!

+6
source share
2 answers

Try the following:

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

select users as a view and then query outside of the selection request

 select name from (select first_name||last_name FROM `users` WHERE frst_name LIKE "%John%" or last name like "%doe%") where name like '%John Doe%' 
0
source

Source: https://habr.com/ru/post/918667/


All Articles