我有一张单词表和一个短语表。我想计算每个单词包含多少个短语。
这个查询给了我一个(单词,短语)对的列表;我该怎么算?有没有更有效的方法呢?
选择words.word,phrase.phrase
从单词左边加入短语
on phrase.phrase rlike concat('(^ | [^ a-z])',words.word,'($ | [^ a-z])');
答案 0 :(得分:0)
使用现有查询,您可以添加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