我有兩個表區域(名稱,編號),城市(地區編號,姓名,人口),我想返回人口最多的前5個城市以及該地區的名稱。這是我的查詢,但它只返回一個區域的5個城市(北在這種情況下),而不是所有地區:限制一列的結果
SELECT region.name AS region_name, city.name AS city_name, city.population AS population
FROM region INNER JOIN (SELECT city.name, city.population, city.region_code
FROM county LIMIT 5) AS city
ON region.code = city.region_code;
所需的輸出:
region_name | city_name | population
------------+----------------------------+------------
NORTH | A | 226338
NORTH | B | 13656
NORTH | C | 9478
NORTH | D | 2464
NORTH | E | 2464
WEST | A | 226338
WEST | B | 13656
WEST | C | 9478
WEST | D | 2464
WEST | E | 2464
感謝您對您的幫助提前:)
它的工作,非常感謝你:) – Mera 2014-11-04 06:01:19