2013-08-24 93 views
0

如何找到每個大洲人口最多的前5個國家/地區。我只是想用低於1個表:從列中選擇最高值

網址: 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; 

感謝, 力

回答

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 

沒有數據庫模式我對它的字段做了一些假設。