mysql查询 - 计算类似术语

时间:2012-05-22 11:32:39

标签: mysql sql

我的MySQL表格如下:

ID  term  timestamp

1   foo    xxxx
2   bar    xxxx
3   bar    xxxx
4   foo    xxxx
5   foo    xxxx
6   foo    xxxx
7   xxx    xxxx

我希望按照该术语的数量顺序生成一个显示最常用术语的列表

即。最终结果:

foo 
bar
xxx

我应该使用什么查询?我做了一些谷歌搜索,但我不太确定我在寻找什么 - COUNT / GROUP BY等。

6 个答案:

答案 0 :(得分:4)

以下SQL查询应该为您的问题提供解决方案。

SELECT term
FROM tablename
GROUP BY term
ORDER BY COUNT(*) DESC

答案 1 :(得分:2)

SELECT COUNT(*), term FROM table GROUP BY term ORDER BY COUNT(*) DESC;

答案 2 :(得分:1)

尝试

SELECT term, Count(*) AS cnt FROM mytable GROUP BY term ORDER BY cnt DESC

答案 3 :(得分:1)

GROUP BY是正确的。试试这个:

SELECT term FROM table
GROUP BY term
ORDER BY COUNT(term) desc

答案 4 :(得分:0)

SELECT TERM
FROM @table
GROUP BY TERM
HAVING COUNT(*) > 5 --replace with number you want
ORDER BY COUNT(*) 

答案 5 :(得分:0)

这个SQL查询应该这样做:

SELECT term FROM my_table GROUP BY term ORDER BY COUNT(term) DESC;