显示具有最相等条目的对象

时间:2014-06-27 15:39:20

标签: mysql sql count max

您好我想展示拥有最多博物馆的国家(德国土地)。

我的表格如下:

+-----------+----------------------+-------------------+--------------------------+---------------+
| MuseumsNR | Name                 | Stadt             | Land                     | Hauptstadt    |
+-----------+----------------------+-------------------+--------------------------+---------------+
|         1 | Museum of Modern Art | New York          | United States of America | Washington DC |
|         2 | Kunstmuseum          | Bern              | Schweiz                  | Bern          |
|         3 | Musée Picasso        | Paris             | Frankreich               | Paris         |
|         4 | Städel               | Frankfurt am Main | Deutschland              | Berlin        |
|         5 | Museum Ludwig        | Köln              | Deutschland              | Berlin        |
+-----------+----------------------+-------------------+--------------------------+---------------+

所以我想要的输出应该是:

+--------------------------+------+
| Land                     |Anzahl|
+--------------------------+------+
| Deutschland              |    2 |
+--------------------------+------+

这就是我的尝试:

SELECT Land, COUNT(Name) Name from  Museum order by Name desc;

输出:

+--------------------------+------+
| Land                     | Name |
+--------------------------+------+
| United States of America |    5 |
+--------------------------+------+

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

SELECT Land, count(*) as Anzahl 
    FROM yourTable 
    GROUP BY Land 
    ORDER BY Anzahl 
    DESC LIMIT 1;`

仅在PostgreSQL中测试过,但在MySQL中应该足够接近。