Suppose I have a column with words:
假设我有一个单词列:
orange
grape
orange
orange
apple
orange
grape
banana
How do I execute a query to get the top 10 words, as well as their count?
如何执行查询以获取前10个单词以及计数?
2 个解决方案
#1
3
SELECT word, COUNT(*) word_cnt
FROM your_table
GROUP BY word
ORDER BY COUNT(*) DESC
LIMIT 10
The GROUP BY
groups by values of word
, the ORDER BY COUNT(*) DESC
gets you the rows with highest count first, and the LIMIT 10
returns the first 10 rows only.
GROUP BY按字的值分组,ORDER BY COUNT(*)DESC首先获取具有最高计数的行,而LIMIT 10仅返回前10行。
#2
3
SELECT word, COUNT(*) AS n
FROM `table`
GROUP BY word
ORDER BY COUNT(*) DESC
LIMIT 10
#1
3
SELECT word, COUNT(*) word_cnt
FROM your_table
GROUP BY word
ORDER BY COUNT(*) DESC
LIMIT 10
The GROUP BY
groups by values of word
, the ORDER BY COUNT(*) DESC
gets you the rows with highest count first, and the LIMIT 10
returns the first 10 rows only.
GROUP BY按字的值分组,ORDER BY COUNT(*)DESC首先获取具有最高计数的行,而LIMIT 10仅返回前10行。
#2
3
SELECT word, COUNT(*) AS n
FROM `table`
GROUP BY word
ORDER BY COUNT(*) DESC
LIMIT 10