MySQL-选择按字母顺序排在第一位的名称

MySQL-选择按字母顺序排在第一位的名称,mysql,sql,Mysql,Sql,我已经开始学习MySQL了 以下是表格world: +-------------+-----------+---------+ | name | continent | area | +-------------+-----------+---------+ | Afghanistan | Asia | 652230 | | Albania | Europe | 2831741 | | Algeria | Africa | 28748

我已经开始学习MySQL了

以下是表格
world

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

按字母顺序列出各大洲和排在第一位的国家的名称

选择的结果必须是:

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

select distinct  w.continent, 
                 (select w2.name 
                  from world w2 
                  where  w.continent =  w2.continent 
                  order by name asc 
                  limit 1) name 
from world w
order by w.continent
SELECT continent, name FROM world ORDER BY name ASC;
试试这个

select distinct  w.continent, 
                 (select w2.name 
                  from world w2 
                  where  w.continent =  w2.continent 
                  order by name asc 
                  limit 1) name 
from world w
order by w.continent
SELECT continent, name FROM world ORDER BY name ASC;

这是一种简单的矛盾:

SELECT continent, MIN(name) AS name
FROM world 
GROUP BY continent
ORDER by continent

如果您需要按字母顺序列出每个大陆,您可以使用

     SELECT * from world ORDER by continent
但是,如果你需要列出你使用的每个国家

     SELECT * from world ORDER by name
如果这是一个来自IMO的练习,那么应该是这样的:

select continent, name from world x
where name = (select name 
                  from world y 
                  where  x.continent =  y.continent 
                  order by name asc 
                  limit 1)
SELECT continent, name FROM world x
WHERE name <= ALL
  (SELECT name FROM world y WHERE y.continent=x.continent)
另外,我现在在那里学习SQL,这篇文章帮助了我。感谢@Parado!)


更新:我找到了答案。如果堆栈可用。

SqlZoo解决方案最好如下所示:

select continent, name from world x
where name = (select name 
                  from world y 
                  where  x.continent =  y.continent 
                  order by name asc 
                  limit 1)
select continent, name from world group by continent order by name
SELECT continent, name FROM world x
WHERE name <= ALL
  (SELECT name FROM world y WHERE y.continent=x.continent)
选择大陆,名称来自world x
WHERE name那么这个sql呢:

select distinct continent, 
      (select name 
       from world y where y.continent = x.continent limit 1 ) as name 
from world x
这是相关/同步查询。

从world x中选择不同的大陆名称
Select distinct continent, name from world x 
where name <=All (Select name from world y where x.continent=y.continent)

其中的名称将列出世界上的每个国家。这将列出世界上的每个国家。不,这将列出bd中的国家或continet,没有指定任何限制,并且比我的更简单:)您不需要ORDER BY语句。您可能不需要它,但是OP需要一个有序的结果集,即使结果当前以正确的顺序返回,它也可能随时更改。这不会对已选择的答案添加任何内容,也不会回答以下问题:OP只对结果进行排序,而OP只希望每个洲有一个结果。为什么
选择内部选择中的top 1
不起作用?请解释您的答案以及它将如何解决问题。请参阅