2012-01-17 161 views
2

我是SQL新手。目前,我正在學習編寫複雜的查詢。關於SQL查詢

我有三張桌子。 國家 - 有國家的名單

-----------+------------- 
CountryId | CountryName 
-----------+------------- 
     1 | India 
     2 | Srilanka 
     3 | Pakistan 
     4 | Bangladesh 
     5 | Nepal 
     6 | America 
     7 | Japan 
     8 | China 
     9 | Russia 
     10 | Canada 
     11 | Australia 
--------------------------------------- 

城市 - 鄉村的城市名單

--------+-------------+----------- 
CityId | CityName | CountryId 
--------+-------------+----------- 
     1 | Chennai  |   1 
     2 | Washington |   6 
     3 | Moscow  |   9 
     4 | Tokyo  |   7 
     5 | Beijing  |   8 
     6 | Sydney  |  11 
     7 | Bangalore |   1 
     8 | Nagercoil |   1 
     9 | AmericaCity |   6 
    10 | Kathmandu |   5 
    11 | Dhaka  |   4 
    12 | Lahore  |   3 
-------------------------------------- 

機場 - 機場在城市

AirportId | AirportName | CityId 
-----------+-------------+-------- 
     1 | Airport1 |  1 
     2 | Airport2 |  4 
     3 | Airport3 |  5 
     4 | Airport4 |  1 
     5 | Airport5 |  6 
     6 | Airport6 |  3 
     7 | Airport7 |  5 
     8 | Airport8 |  7 
     9 | Airport9 |  6 
     10 | Airport10 |  3 
     11 | Airport11 |  11 
     12 | Airport12 |  10 
     13 | Airport13 |  12 
--------------------------------- 

問題清單:我想找回所有有機場數量的國家,如

Output: 
countryName Airports 
India   3 
Srilanka   0 
......... etc. 
+0

這不是功課,是嗎? – dasblinkenlight 2012-01-17 12:01:05

回答

0

嘗試:

SELECT 
    Country.CountryId, 
    Country.CountryName, 
    count(AirportID) AS Airports 
FROM 
    Country 
    LEFT JOIN city ON Country.CountryId=city.CountryId 
    LEFT JOIN Airport ON city.CityId=Airport.CityId 
GROUP BY Country.CountryId 
+0

謝謝馬克。這一個完美的作品....感謝您的支持。 – user1153769 2012-01-23 12:21:46

1
select 
    c.CountryName, 
    SUM(
    select count(*) from Airport a 
    inner join City city on city.CityId = a.CityId 
    where city.CountryId = c.CountryId 
    ) as Airports 
from 
    Country c 
+0

這不會在MyISAM/Linux上運行:表名在這裏區分大小寫 – 2012-01-17 12:06:54

+0

我不知道OP是否在開始時創建了所有帶有大寫字母的表(如Country,Airport),並且忘記保持該轉換關鍵城市,但關於「countryName」你是完全正確的。 – Matten 2012-01-17 12:35:10

+0

這不是批評你,只是一個提示 - 如果你修復你的答案,我會刪除評論 – 2012-01-17 12:36:58

0
SELECT 
    Country.CountryName, 
    count(*) AS Airports 
FROM 
    Country 
    INNER JOIN city ON Country.CountryId=city.CountryId 
    INNER JOIN Airport ON city.CityId=Airport.CityId 
GROUP BY Country.CountryId 
2
SELECT a.AirportName, co.CountryName, COUNT(co.name) AS count 
FROM Airports as a 
LEFT JOIN City as c ON a.CityId = c.CityId 
LEFT JOIN Country as co ON c.CountryId = co.CountryId 
GROUP BY co.CountryId 
+1

其他答案不涉及沒有城市機場的國家。這一個;對於沒有機場的國家它將返回0。 – xQbert 2012-01-17 12:03:05

+0

@xQbert:沒有 - 它現在正在**從**機場加入城市到國家,因此它將檢索機場與城市或國家。將左連接更改爲右連接可以解決此問題,但計數還需要以機場名稱爲準,而不是國家名稱。 – 2012-01-17 12:54:19

1
SELECT CountryName, COUNT(CountryName) AS Airports 

FROM Airports INNER JOIN City ON Airports.CityId = City.CityId 
       INNER JOIN Country ON City.CountryId = Country.CountryId 

GROUP BY CountryId 

希望這將是有用的,你