我有一个表格:items(id,description)。在我的程序中,我输入一个单词列表(word1到wordN),我需要计算这些单词在表格的每个描述中出现的数量,并根据该数字对结果进行排序。这是我的解决方案,但我很乐意就如何提高性能提出建议。谢谢。通过+ order by + count提高工作效率union all + group
SELECT x, COUNT(*)
FROM (SELECT description as x, id FROM items where description LIKE '%word1%'
UNION ALL
SELECT description as x, id FROM items where description LIKE '%word2%'
UNION ALL
...
UNION ALL
SELECT description as x, id FROM items where description LIKE '%wordN%')
GROUP BY (id)
ORDER BY COUNT(*) DESC
这产生了一个不同的结果集:如果两个单词匹配一个描述原始查询将计数2,而你的计数为1. – APC 2012-04-10 11:22:30