如何在MYSQL中执行此查询?

时间:2010-03-15 07:28:54

标签: mysql database

假设我有一个包含单词的列:

orange
grape
orange
orange
apple
orange
grape
banana

如何执行查询以获取前10个单词及其计数?

2 个答案:

答案 0 :(得分:3)

SELECT word, COUNT(*) word_cnt
FROM your_table
GROUP BY word
ORDER BY COUNT(*) DESC
LIMIT 10

GROUP BY组的值wordORDER BY COUNT(*) DESC获取最先计数最多的行,LIMIT 10仅返回前10行。

答案 1 :(得分:3)

SELECT   word, COUNT(*) AS n
FROM     `table`
GROUP BY word
ORDER BY COUNT(*) DESC
LIMIT 10