我有一个包含列的 user_table
- 名字
- 姓氏
- 联系电话
对于自动完成 UI,用户可以自由搜索任何内容。
我希望允许用户通过类似查询按全名和联系人号码进行搜索。
我写的查询就像,
select CONCAT(u.first_name ,' ', u.last_name) as full_name, contact_number from user_table u where contact_number like '%value%' HAVING full_name like '%value%';
它不起作用,因为 contact_number 和 full_name 的值都相同。如果我删除 contact_number 或已关闭,则部分它适用于这两种情况。我无法在 where 和having 之间放置 OR 子句。
最佳答案
正如帖子中提到的
I want to allow user to search by full_name as well as contact_number with like query.
您可以使用OR
WHERE
中的运算符如果您想使用 contact_number
进行搜索,则如下所示和full_name
select CONCAT(u.first_name ,' ', u.last_name) as full_name
from user_table u
where contact_number like '%value%'
OR CONCAT(u.first_name ,' ', u.last_name) like '%value%';
或
select CONCAT(u.first_name ,' ', u.last_name) as full_name
from user_table u
where contact_number like '%value%'
OR u.first_name like '%value%'
OR u.last_name like '%value%';
关于MYSQL HAVING 和 WHERE 带条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55898611/