gpt4 book ai didi

mysql - GROUP BY mySQL 语句

转载 作者:行者123 更新时间:2023-11-29 06:38:21 25 4
gpt4 key购买 nike

使用我的 SQL 列表,显示每个国家/地区的城市数量。你认为这个语句的意思是统计每个国家的城市数量吗?

+----+----------------+---------+---------------+------------+
| id | name | country | original-name | population |
+----+----------------+---------+---------------+------------+
| 1 | Kabul | AFG | Kabol | 1780000 |
| 2 | Qandahar | AFG | Qandahar | 237500 |
| 3 | Herat | AFG | Herat | 186800 |
| 4 | Mazar-e-Sharif | AFG | Balkh | 127800 |
| 5 | Amsterdam | NLD | Noord-Holland | 731200 |
| 6 | Rotterdam | NLD | Zuid-Holland | 593321 |
| 7 | Haag | NLD | Zuid-Holland | 440900 |
| 8 | Utrecht | NLD | Utrecht | 234323 |
| 9 | Eindhoven | NLD | Noord-Brabant | 201843 |
| 10 | Tilburg | NLD | Noord-Brabant | 193238 |
+----+----------------+---------+---------------+------------+

我的代码可以工作,但超过 4000 行:

SELECT name, country 
FROM city
GROUP BY name, country
ORDER BY country ASC;

最佳答案

您只需按国家/地区分组:

SELECT country, COUNT(*) as count_of_cities_per_country
FROM city
GROUP BY country
ORDER BY country ASC;

关于mysql - GROUP BY mySQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52794711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com