MySQL计数出现次数大于2

时间:2010-10-14 20:31:23

标签: mysql

我有以下表结构

+  id  +  word  +
+------+--------+

表中填充了给定文本的低级cas中的单词,因此文本

你好再见

会导致

+  id  +  word  +
+------+--------+
+   1  + hello  +
+------+--------+
+   2  + bye    +
+------+--------+
+   3  + hello  +
+------+--------+

我想创建一个SELECT查询,它将返回在表中至少重复两次的单词数(如hello)

SELECT COUNT(id) FROM words WHERE (SELECT COUNT(words.word))>1

当桌子很大时,这当然是错误的并且超级重载。关于如何达到这个目的的任何想法?在上面的给定示例中,我希望1

4 个答案:

答案 0 :(得分:193)

要获得不止一次出现的单词列表及其发生频率,请使用GROUP BY和HAVING的组合:

SELECT word, COUNT(*) AS cnt
FROM words
GROUP BY word
HAVING cnt > 1

要查找上述结果集中的单词数,请将其用作子查询并计算外部查询中的行数:

SELECT COUNT(*)
FROM
(
    SELECT NULL
    FROM words
    GROUP BY word
    HAVING COUNT(*) > 1
) T1

答案 1 :(得分:17)

SELECT count(word) as count 
FROM words 
GROUP BY word
HAVING count >= 2;

答案 2 :(得分:6)

SELECT word, COUNT(*) FROM words GROUP by word HAVING COUNT(*) > 1

答案 3 :(得分:1)

HAVING 选项可用于此目的,查询应为

SELECT word, COUNT(*) FROM words 
GROUP BY word
HAVING COUNT(*) > 1;