使用SQL一次搜索多个事物的最有效方法?

时间:2012-03-13 16:20:33

标签: mysql sql

例如,我想在标题/描述中搜索“Sprite”,“Pepsi”或“Coke”的所有条目,并输出最后3个结果。

------------------------------------------------
| 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 个答案:

答案 0 :(得分:1)

SELECT *
FROM `table`
WHERE description regexp ('sprite|pepsi|coke')
ORDER BY 'id' DESC