我有一个带列的user_table
- first_name
- last_name
- contact_number
对于自动完成 UI,用户可以自由搜索任何内容。
我想允许用户按full_name搜索以及使用类似查询contact_number。
我写的查询是这样的,
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或关闭,那么部分它适用于这两种情况。我无法在何处和拥有之间放置 OR 子句。
如帖子中所述
我想允许用户按full_name搜索以及使用类似查询contact_number。
如果要使用contact_number
和full_name
进行搜索,可以在WHERE
子句中使用OR
运算符
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%';
select CONCAT(p.first_name ,' ', p.last_name) as full_name, contact_number from user_table p HAVING full_name like '%Nimesh S%' or contact_number like '%Nimesh S%';
或
select CONCAT(p.first_name ,' ', p.last_name) as full_name, contact_number from user_table p HAVING full_name like '%888%' or contact_number like '%888%';
或 [广义查询]
select CONCAT(p.first_name ,' ', p.last_name) as full_name, contact_number from user_table p HAVING full_name like '%value%' or contact_number like '%value%';