0

我想从 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

什么样的查询可以做到这一点?

4

3 回答 3

2
SELECT Town, COUNT(*) AS cnt FROM my_table GROUP BY Town ORDER BY cnt DESC LIMIT 10
于 2012-05-21T07:49:30.323 回答
0

试试这个 -

SELECT town,
       count(*)
FROM tablename
GROUP BY town;
于 2012-05-21T07:50:20.037 回答
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;
于 2012-05-21T07:52:09.563 回答