小编典典

MySQL-选择按字母顺序排列的名字

sql

我已经开始学习MySQL。

这是表格world

+-------------+-----------+---------+
|    name     | continent |  area   |
+-------------+-----------+---------+
| Afghanistan | Asia      | 652230  |
| Albania     | Europe    | 2831741 |
| Algeria     | Africa    | 28748   |
| ...         | ...       | ...     |
+-------------+-----------+---------+

我需要:

列出每个大洲以及按字母顺序排在首位的国家/地区的名称

SELECT的结果必须是:

+---------------+---------------------+
|   continent   |         name        |
+---------------+---------------------+
| Africa        | Algeria             |
| Asia          | Afghanistan         |
| Caribbean     | Antigua and Barbuda |
| Eurasia       | Armenia             |
| Europe        | Albania             |
| North America | Belize              |
| Oceania       | Australia           |
| South America | Argentina           |
+---------------+---------------------+

阅读 157

收藏
2021-03-17

共1个答案

小编典典

这是一个简单的聚合:

SELECT continent, MIN(name) AS name
FROM world 
GROUP BY continent
ORDER by continent
2021-03-17