2012-01-17 122 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 

希望这将是有用的,你