MySQL通过一组空格分隔值查询搜索

时间:2013-01-31 14:05:51

标签: mysql

我有一张示例表:

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 

欣赏任何想法!谢谢!

2 个答案:

答案 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)

SQLFiddle

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