【发布时间】:2012-04-10 10:56:31
【问题描述】:
我有一张表:items(id,description)。在我的程序中,我得到一个单词列表(word1 到 wordN)作为输入,我需要计算这些单词中有多少出现在表中的每个描述中,并根据该数字对结果进行排序。这是我的解决方案,但我欢迎有关如何提高性能的建议。谢谢。
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
【问题讨论】:
标签: sql group-by sql-order-by union-all