首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL查询,三个表,外部连接

问MySQL查询,三个表,外部连接
EN

Stack Overflow用户
提问于 2016-04-07 11:55:31
回答 3查看 43关注 0票数 0

编辑:回答:

代码语言:javascript
复制
SELECT c.Name FROM Country c
WHERE NOT EXISTS 
( SELECT * FROM City 
JOIN Building ON City.CityID = Building.CityID 
WHERE City.CountryID = c.CountryID);

谢谢你们的时间伙计们。

我有这样的模式:

​

​

我这样做是为了选择所有没有建筑的城市。

代码语言:javascript
复制
select * from City c 
  left join Building b on b.CityID = c.CityID
  where b.CityID is null or c.CityID is null
  union all
  select * from City c 
  right join Building b on b.CityID = c.CityID 
  where b.CityID is null or c.CityID is null;

如何选择没有建筑物的国家名称?

表说明:

​

  1. 数据示例:

​

​

2.预期产出:英格兰

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-07 12:23:41

试试这个,告诉我

代码语言:javascript
复制
 select * from country where CountryID not in
 (select CountryID from country as c   join city as t on c.CountryID =t.CountryID join building as b on t.CityID = b.CityID )
票数 1
EN

Stack Overflow用户

发布于 2016-04-07 13:39:45

请考虑以下几点:

代码语言:javascript
复制
SELECT o.countryid
     , o.name country
     , i.cityID
     , i.name city
     , b.buildingid 
  FROM country o 
  LEFT 
  JOIN city i 
    ON i.countryid = o.countryid 
  LEFT 
  JOIN building b 
    ON b.cityid = i.cityid;
+-----------+---------+--------+-----------+------------+
| countryid | country | cityID | city      | buildingid |
+-----------+---------+--------+-----------+------------+
|         2 | England |      2 | London    |       NULL |
|         2 | England |      5 | Liverpool |       NULL |
|         4 | France  |      4 | Paris     |          5 |
|         3 | Germany |      3 | Berlin    |          3 |
|         3 | Germany |      3 | Berlin    |          4 |
|         1 | Poland  |      1 | Warsaw    |          1 |
|         1 | Poland  |      1 | Warsaw    |          2 |
+-----------+---------+--------+-----------+------------+

这个问题的最后一部分是留给读者的练习。

票数 0
EN

Stack Overflow用户

发布于 2016-04-07 12:24:35

代码语言:javascript
复制
SELECT Country.Name
FROM Country
JOIN City ON (Country.CountryID = City.CountryID)
WHERE City.CityID =
(SELECT City.CityID FROM City
JOIN Building
ON (City.CityID = Building.CityID)
WHERE City.CityID NOT IN Building.CityID)
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36475442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档