我有一张示例表:
Firstname Lastname Company Age
Peter Johnson Google 28
John Wayne Yahoo 30
我需要编写一个MySQL查询,它通过一组由空格分隔的值来执行搜索。像这样:
Exec find_records 'Bill Johnson Google 30';
查询应返回:
Firstname Lastname Company Age
Peter Johnson Google
John Wayne Yahoo
欣赏任何想法!谢谢!
答案 0 :(得分:1)
如果您使用的是MyISAM表,则可以在与搜索和查询相关的列上添加全文索引:
SELECT
firstname,
lastname,
company,
age,
MATCH(firstname, lastname, company, age) AGAINST('Bill Johnson Google 30') as rank
FROM
your_table
HAVING
rank > 0
ORDER BY
rank DESC
答案 1 :(得分:0)
select *
from t
where
FIND_IN_SET(FirstName,REPLACE('Bill Johnson Google 30',' ',','))>0
or
FIND_IN_SET(Lastname,REPLACE('Bill Johnson Google 30',' ',','))>0
or
FIND_IN_SET(Company,REPLACE('Bill Johnson Google 30',' ',','))>0
or
FIND_IN_SET(Age,REPLACE('Bill Johnson Google 30',' ',','))>0