私はそれを考え出した。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";