我想到了。SELECT DISTINCT ON (country.name) country.name AS country... 工作
我正在查询每个国家/地区的最大城市(就人口而言)。问题是给定国家/地区的某些城市的人口数量相同,因此查询返回的每个国家/地区不止一个城市。(我试过 SELECT DISTINCT country.name,但它给了我相同的输出)
有任何想法吗?
这是我的代码:
$query = "SELECT country.name AS country, largest_city, sq.pop AS population
FROM lab6.country INNER JOIN
(SELECT MAX(city.population) AS pop, country_code
FROM lab6.city
GROUP BY country_code) AS sq
USING (country_code)
INNER JOIN
(SELECT city.name AS largest_city, city.population
FROM lab6.city) AS sq1
ON (sq1.population = sq.pop)
ORDER BY country.name ASC";