MySQL - CONCAT two fields and use them in WHERE clause

假如想象 提交于 2019-12-23 07:54:51

问题


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!


回答1:


Try this ::

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



回答2:


pick up the users as view then query outside select query

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%'


来源:https://stackoverflow.com/questions/11140647/mysql-concat-two-fields-and-use-them-in-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!