我有一张单词表和一张短语表。我想计算每个单词包含多少个短语。
这个查询给了我一个(单词,短语)对的列表;我如何得到计数?有没有更有效的方法来做到这一点?
选择 words.word、phrase.phrase
from words left join 词组
关于phrase.phrase rlike concat('(^|[^az])', words.word, '($|[^az])');
使用您现有的查询,您可以添加一个GROUP BY
子句和一个COUNT
:
SELECT
words.word,
COUNT(phrases.phrase) as Count
FROM
words
LEFT JOIN phrases
ON phrases.phrase RLIKE CONCAT('(^|[^a-z])', words.word, '($|[^a-z])')
GROUP BY
words.word
或者,您可以使用子查询而不是连接来实现此目的:
SELECT
words.word,
(SELECT COUNT(*)
FROM phrases
WHERE phrase RLIKE CONCAT('(^|[^a-z])', words.word, '($|[^a-z])')
) AS Count
FROM words