我试图了解如何解决此问题,但无法理解。
问题是: 每个国家的人口第二大城市。
这是我到目前为止所写的内容:
SELECT c1.Name, c1.population
FROM city c1
WHERE c1.population =
(SELECT MAX(c1.population)
FROM city c1
WHERE c1.population GROUP BY c1.CountryCode <
(SELECT MAX(c1.population) FROM city c1 GROUP BY c1.CountryCode));
这不起作用,无法获得答案。
模式:
Country(NAME,CODE,CONTINENT, REGION, SUFACEAREA, INDEPYEAR, POPULATION, LIFEEXPECT, GNP, GNPID, LOCALNAME)
CountryLanguage(Percentage, CountryCode, Language)
City(ID, NAME, COUNTRYCODE, DISTRICT, POPULATION)
我希望输出结果能够为我查询每个国家中人口最多的城市的结果。
答案 0 :(得分:0)
您没有提供city
表的确切架构,所以我即兴使用。这应该可以解决问题:
# City table
CREATE TABLE city (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(25),
population INT
);
SELECT
c2.*
FROM
city AS c1
JOIN city AS c2 ON c2.population <c1.population
GROUP BY c2.id
ORDER BY c2.population DESC
LIMIT 1;
希望这会有所帮助,