我想从MySQL数据库的表中获得某种前十名列表。 这是一个例子。假设我有这张表:
ID Username Town
1 foo Munich
2 bar Kolding
3 herp Bordeaux
4 derp Bordeaux
5 test Cologne
6 bla Munich
7 blob Bordeaux
现在,我想获得'Town'中最常见的条目,如下:
Bordeaux 3
Munich 2
Cologne 1
Kolding 1
什么样的查询可以做到这一点?
答案 0 :(得分:2)
SELECT Town, COUNT(*) AS cnt FROM my_table GROUP BY Town ORDER BY cnt DESC LIMIT 10
答案 1 :(得分:0)
试试这个 -
SELECT town,
count(*)
FROM tablename
GROUP BY town;
答案 2 :(得分:0)
这个怎么样。
SELECT Town, Count(1) as TownCount
FROM myTable
GROUP BY Town
ORDER BY TownCount DESC;
如果您只想要10条记录,请使用
SELECT Town, Count(1) as TownCount
FROM myTable
GROUP BY Town
ORDER BY TownCount DESC
LIMIT 10;