2012-03-13 78 views
0

例如,我想在标题/说明中搜索“Sprite”,“Pepsi”或“Coke”的所有条目,并输出最后3个结果。用SQL一次搜索多个事物的最有效方法?

------------------------------------------------ 
| ID | Title     | Description  | 
------------------------------------------------ 
| 1 | text text SPRITE text | text PEPSI xxx | 
| 2 | text text text text | text text text | 
| 3 | text text SPRITE text | text COKE xxxx | 
| 4 | text text text text | text text text | 
| 5 | text PEPSI text text | text COKE xxxx | 
| 6 | text COKE text text | text COKE xxxx | 
| 7 | text text text text | text text text | 
| 8 | text text text text | text text text | 
------------------------------------------------ 

这是最高效的查询吗?

SELECT * 
FROM `table` 
WHERE title LIKE '%Sprite%' OR description LIKE '%Sprite%' 
     OR title LIKE '%Pepsi%' OR description LIKE '%Pepsi%' 
     OR title LIKE '%Coke%' OR description LIKE '%Coke%' 
ORDER BY 'id' DESC 
LIMIT 3; 

或者有没有办法与MATCH AGAINST做到这一点? (我不能找到一种方法)..

输出示例:

------------------------------------------------ 
| 6 | text COKE text text | text COKE xxxx | 
| 5 | text PEPSI text text | text COKE xxxx | 
| 3 | text text SPRITE text | text COKE xxxx | 
------------------------------------------------ 
+1

您最好的选择是使用全文搜索服务。例如,狮身人面像。 – 2012-03-13 16:22:22

+2

如果你有全文索引,你应该可以用MATCH(title,description)AGAINST('sprite')等来做。 – 2012-03-13 16:24:36

+0

如何一次匹配多个搜索字词? – supercoolville 2012-03-13 17:24:09

回答

1
SELECT * 
FROM `table` 
WHERE description regexp ('sprite|pepsi|coke') 
ORDER BY 'id' DESC 
相关问题