从列中选择最高值

时间:2013-08-24 02:14:06

标签: mysql sql

如何找到每个大洲人口最多的前5个国家。我只想使用1下表:

URL: http://dev.mysql.com/doc/index-other.html

数据库: 世界数据库(MyISAM版本,用于MySQL认证和培训)

以下是我提出的建议:

select Continent,
substring_index(
GROUP_CONCAT(distinct cast(Name as char)), ',', 5)
From
country
group by
Continent,Name;

谢谢, 力

1 个答案:

答案 0 :(得分:0)

这个是correlated sub-query

SELECT c.name, c.continent 
WHERE population IN (SELECT population 
                    FROM country c1
                    WHERE c.continent = c1.continent
                    ORDER by population
                    LIMIT 5)
FROM country c

没有数据库模式我对其字段做了一些假设。