[英]Selecting top values from columns
How can I find top 5 countries with highest population per continent. 如何找到每个大陆人口最多的前5个国家。 I just want to use below 1 table:
我只想使用下面的1个表:
URL: http://dev.mysql.com/doc/index-other.html 网址: http : //dev.mysql.com/doc/index-other.html
Database: world database (MyISAM version, used in MySQL certifications and training) 数据库:世界数据库(MyISAM版本,用于MySQL认证和培训)
Below is what I came up with: 以下是我的想法:
select Continent,
substring_index(
GROUP_CONCAT(distinct cast(Name as char)), ',', 5)
From
country
group by
Continent,Name;
Thanks, Rio 谢谢,里约热内卢
This one is with correlated sub-query
: 这是与
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
Not having database schema I made some assumptions about its fields. 没有数据库架构,我对其字段进行了一些假设。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.