2012-01-17 8 views
2

私はSQLを初めて使用しています。現在、複雑なクエリを書くことを学んでいます。SQLクエリについて

私は3つのテーブルを持っています。 Country--は、国のリストを持っている

-----------+------------- 
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

答えて

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

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では実行されません。テーブル名は大文字と小文字が区別されます –

+0

OPが大文字の頭文字(Country、Airportなど)を持つすべてのテーブルを作成していて、都市のキーですが、 "countryName"に関してはまったく正しいです。 – Matten

+0

これはあなたを批判していませんでした。あなたの答えを修正すれば、私はコメント –

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

+0

@xQbert:いいえ - 現在、**空港から市街地に**加わりましたので、都市または国の空港を検索します。左のジョインを右のジョインに変更すると修正されますが、カウントは国名ではなく空港名である必要があります。 –

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 

これは

関連する問題