根据第三个 table 的值查找不存在的地方
Find where not exists, based on value from third table
我有一个名为 Cities
的 table,如下所示:
+--------+-------------+-------+----------+------------+
| CityID | City | State | TimeZone | UserAccess |
+--------+-------------+-------+----------+------------+
| 1 | Albany | NY | 4 | 1 |
| 2 | Allentown | PA | 6 | 1 |
| 3 | Albuquerque | NM | 4 | 1 |
| 4 | Amarillo | TX | 3 | 1 |
| 5 | Atlanta | GA | 4 | 1 |
+--------+-------------+-------+----------+------------+
我还有一个叫 CitiesToRegions
的 table,如下所示:
+--------+----------+
| CityID | RegionID |
+--------+----------+
| 1 | 14 |
| 1 | 15 |
| 2 | 14 |
| 3 | 11 |
| 4 | 12 |
| 4 | 13 |
| 5 | 12 |
| 5 | 13 |
+--------+----------+
我有一个 Regions
table 如下所示:
+----------+-----------+--------+
| RegionID | Region | TypeID |
+----------+-----------+--------+
| 10 | West | 1 |
| 11 | West | 2 |
| 12 | South | 1 |
| 13 | South | 2 |
| 14 | Northeast | 1 |
| 15 | Northeast | 2 |
+----------+-----------+--------+
我想做的是编写一个查询,以便我可以根据 Regions
[=41] 的 TypeID
来查看我在哪里缺少 CitiesToRegions
指定=].这是我到目前为止没有返回任何内容,我确定这是因为我的第一个连接没有发现空值,因为 CityID
分配了 RegionID
。我只是不知道如何真正写出我要找的东西。
SELECT DISTINCT Cities.CityID
FROM Cities
INNER JOIN CitiesToRegions on Cities.CityID = CitiesToRegions.CityID
FULL JOIN Regions on CitiesToRegions.RegionID = Regions.RegionID
WHERE (CitiesToRegions.RegionID is null) AND (Cities.UserAccess=1) AND (Regions.TypeID != 1)
我希望看到的查询结果如下,因为它是唯一没有 RegionID
或 Regions.TypeID = 1
的 CityID
。
+--------+
| CityID |
+--------+
| 3 |
+--------+
注意:我不关心看到缺少 TypeID
of 2 的那些,因为我会 运行 相同的查询并将 TypeID
作为变量传递:
下面应该满足您的需要 - 我发现 not exists
子查询比复杂的连接更清楚地获得正确的逻辑。
select *
from Cities C
where not exists (
select 1
from CitiesToRegions CR
inner join Regions R on CR.RegionID = R.RegionID and R.TypeID = 1
where CR.CityID = C.CityID
)
我有一个名为 Cities
的 table,如下所示:
+--------+-------------+-------+----------+------------+
| CityID | City | State | TimeZone | UserAccess |
+--------+-------------+-------+----------+------------+
| 1 | Albany | NY | 4 | 1 |
| 2 | Allentown | PA | 6 | 1 |
| 3 | Albuquerque | NM | 4 | 1 |
| 4 | Amarillo | TX | 3 | 1 |
| 5 | Atlanta | GA | 4 | 1 |
+--------+-------------+-------+----------+------------+
我还有一个叫 CitiesToRegions
的 table,如下所示:
+--------+----------+
| CityID | RegionID |
+--------+----------+
| 1 | 14 |
| 1 | 15 |
| 2 | 14 |
| 3 | 11 |
| 4 | 12 |
| 4 | 13 |
| 5 | 12 |
| 5 | 13 |
+--------+----------+
我有一个 Regions
table 如下所示:
+----------+-----------+--------+
| RegionID | Region | TypeID |
+----------+-----------+--------+
| 10 | West | 1 |
| 11 | West | 2 |
| 12 | South | 1 |
| 13 | South | 2 |
| 14 | Northeast | 1 |
| 15 | Northeast | 2 |
+----------+-----------+--------+
我想做的是编写一个查询,以便我可以根据 Regions
[=41] 的 TypeID
来查看我在哪里缺少 CitiesToRegions
指定=].这是我到目前为止没有返回任何内容,我确定这是因为我的第一个连接没有发现空值,因为 CityID
分配了 RegionID
。我只是不知道如何真正写出我要找的东西。
SELECT DISTINCT Cities.CityID
FROM Cities
INNER JOIN CitiesToRegions on Cities.CityID = CitiesToRegions.CityID
FULL JOIN Regions on CitiesToRegions.RegionID = Regions.RegionID
WHERE (CitiesToRegions.RegionID is null) AND (Cities.UserAccess=1) AND (Regions.TypeID != 1)
我希望看到的查询结果如下,因为它是唯一没有 RegionID
或 Regions.TypeID = 1
的 CityID
。
+--------+
| CityID |
+--------+
| 3 |
+--------+
注意:我不关心看到缺少 TypeID
of 2 的那些,因为我会 运行 相同的查询并将 TypeID
作为变量传递:
下面应该满足您的需要 - 我发现 not exists
子查询比复杂的连接更清楚地获得正确的逻辑。
select *
from Cities C
where not exists (
select 1
from CitiesToRegions CR
inner join Regions R on CR.RegionID = R.RegionID and R.TypeID = 1
where CR.CityID = C.CityID
)