在 ORDER BY 之后获得 MySQL 中的第一项

After an ORDER BY get the first item in MySQL

我正在使用 GROUP BY 来显示来自 table 的一般信息。

SELECT * FROM table GROUP BY Continent

数据如下:

|--------|-----------|-----------|
| Id     | Continent |    Fruits |
|--------|-----------|-----------|
|      1 |    Africa |    Banana |
|      1 |    Africa |    Cherry |
|      1 |    Mexico |     Apple |
|      1 |    Mexico |      Pear |
|      1 |    Europa | Blueberry |
|      1 |    Europa |    Orange |
|      1 |    Europa |      Kiwi |
|--------|-----------|-----------|

现在我怎样才能从每个大陆获得第一个水果?

这是我试过的:

SELECT *, MIN(Fruits) AS FirstFruit FROM table GROUP BY Continent

谢谢。

如果您只想在结果集中包含两列,那么聚合就足够了:

select continent, min(fruits) as firstfruit 
from mytable 
group by continent

另一方面,如果您还对其他列感兴趣,则仅靠聚合无法为您提供所需的结果。一种选择使用相关子查询来过滤 table:

select t.*
from mytable t
where t.fruits = (select min(t1.fruits) from mytable t1 where t1.continent = t.continent)