MySQL GROUP BY Ver 8 vs Ver 5.X
MySQL GROUP BY Ver 8 vs Ver 5.X
美好的一天,
我是 MySQL 的新手,我一直在研究它。我一直在关注一个使用 ver 5.X 的非常古老的教程。我在我的虚拟机上使用 8.0.29 版。使用示例世界数据库时,我在使用以下查询按大陆分组时遇到问题。
SELECT Continent, Region, SUM(Popuilation) AS TotalPop FROM country GROUP BY Contient, Region;
+---------------+---------------------------+------------+
| Continent | Region | TotalPop |
+---------------+---------------------------+------------+
| North America | Caribbean | 38140000 |
| Asia | Southern and Central Asia | 1490776000 |
| Africa | Central Africa | 95652000 |
| Europe | Southern Europe | 144674200 |
| Asia | Middle East | 188380700 |
| South America | South America | 345780000 |
| Oceania | Polynesia | 633050 |
| Antarctica | Antarctica | 0 |
| Oceania | Australia and New Zealand | 22753100 |
| Europe | Western Europe | 183247600 |
| Africa | Eastern Africa | 246999000 |
| Africa | Western Africa | 221672000 |
| Europe | Eastern Europe | 307026000 |
| North America | Central America | 135221000 |
| North America | North America | 309632000 |
| Asia | Southeast Asia | 518541000 |
| Africa | Southern Africa | 46886000 |
| Asia | Eastern Asia | 1507328000 |
| Europe | Nordic Countries | 24166400 |
| Africa | Northern Africa | 173266000 |
| Europe | Baltic Countries | 7561900 |
| Oceania | Melanesia | 6472000 |
| Oceania | Micronesia | 543000 |
| Europe | British Islands | 63398500 |
| Oceania | Micronesia/Caribbean | 0 |
+---------------+---------------------------+------------+
25 rows in set (0.00 sec)
我做错了什么? VTC 教程显示 continent 的分组很好。
对于我在这个话题上的笨拙,我深表歉意...
除了 Contient
中的拼写错误外,您的查询是正确的。但是,如果您希望结果集按照各大洲排序,则必须在查询中添加一个 order by 子句。
SELECT Continent, Region, SUM(Population) as TotalPop
FROM country
GROUP BY Continent, Region
ORDER BY Continent, Region;
美好的一天,
我是 MySQL 的新手,我一直在研究它。我一直在关注一个使用 ver 5.X 的非常古老的教程。我在我的虚拟机上使用 8.0.29 版。使用示例世界数据库时,我在使用以下查询按大陆分组时遇到问题。
SELECT Continent, Region, SUM(Popuilation) AS TotalPop FROM country GROUP BY Contient, Region;
+---------------+---------------------------+------------+
| Continent | Region | TotalPop |
+---------------+---------------------------+------------+
| North America | Caribbean | 38140000 |
| Asia | Southern and Central Asia | 1490776000 |
| Africa | Central Africa | 95652000 |
| Europe | Southern Europe | 144674200 |
| Asia | Middle East | 188380700 |
| South America | South America | 345780000 |
| Oceania | Polynesia | 633050 |
| Antarctica | Antarctica | 0 |
| Oceania | Australia and New Zealand | 22753100 |
| Europe | Western Europe | 183247600 |
| Africa | Eastern Africa | 246999000 |
| Africa | Western Africa | 221672000 |
| Europe | Eastern Europe | 307026000 |
| North America | Central America | 135221000 |
| North America | North America | 309632000 |
| Asia | Southeast Asia | 518541000 |
| Africa | Southern Africa | 46886000 |
| Asia | Eastern Asia | 1507328000 |
| Europe | Nordic Countries | 24166400 |
| Africa | Northern Africa | 173266000 |
| Europe | Baltic Countries | 7561900 |
| Oceania | Melanesia | 6472000 |
| Oceania | Micronesia | 543000 |
| Europe | British Islands | 63398500 |
| Oceania | Micronesia/Caribbean | 0 |
+---------------+---------------------------+------------+
25 rows in set (0.00 sec)
我做错了什么? VTC 教程显示 continent 的分组很好。
对于我在这个话题上的笨拙,我深表歉意...
除了 Contient
中的拼写错误外,您的查询是正确的。但是,如果您希望结果集按照各大洲排序,则必须在查询中添加一个 order by 子句。
SELECT Continent, Region, SUM(Population) as TotalPop
FROM country
GROUP BY Continent, Region
ORDER BY Continent, Region;