2

我有一个示例表:

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 

将不胜感激任何想法!谢谢!

4

2 回答 2

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
于 2013-01-31T14:14:11.280 回答
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
于 2013-01-31T14:24:50.020 回答