表:
Country ------- PK CountryID Name City ------- PK CityID FK CountryID Name Airport -------- PK AirportID FK CityID Name
我的任务是选择没有机场的国家/地区的名称。
我可以想象只有一个解决方案有EXCEPT(或MINUS)
SELECT Country.Name
FROM Country EXCEPT (SELECT DISTINCT Country.Name FROM Country, City, Airport
WHERE City.CountryID = Country.CountryID AND Airport.CityID = City.CityID);
但是有可能不使用EXCEPT而是像IS NULL那样吗?
答案 0 :(得分:4)
SELECT cn.CountryID
FROM Country cn
LEFT JOIN City ct ON cn.CountryID = ct.CountryID
LEFT JOIN Airport ar on ar.CityID=ct.CityID
WHERE ar.AirportID is null
答案 1 :(得分:2)
如果您需要使用IS NULL
进行此查询,请尝试以下查询:
select ct.CountryId,max(ct.Name) from Country ct
left join City c on ct.CountryId=c.CountryId
left join Airport a on a.CityId=c.CityID
group by ct.CountryId
HAVING max(a.AirportID) IS NULL
答案 2 :(得分:1)
你可以使用它,但它与你的语法基本相同。
SELECT Country.Name
FROM Country
Where Country.Name Not IN
(
SELECT DISTINCT Country.Name FROM Country, City, Airport
WHERE City.CountryID = Country.CountryID AND Airport.CityID = City.CityID
);