在 SQL 服务器中使用多个 NOT IN
Using multiple NOT IN in SQL Server
我有两个这样的 table:
Table 1: City
- 主键: ID_City
:
| City | ID_City | ID_State|
+-----------+---------+---------+
| Chennai | 1 | 2 |
| Bengaluru | 2 | 1 |
| Lucknow | 3 | 3 |
| . | . | . |
| . | . | . |
我正在向现有数据库添加新城市,同时使用 NOT IN 检查现有城市。 @table 看起来像这样:
Table 1: @table
| City | ID_State|Zip_Code|
+-----------+---------+--------+
| Chennai | 2 |00001 |
| Chennai | 2 |00002 |
| Chennai | 2 |00003 |
| Bengaluru | 1 |10011 |
| Bengaluru | 1 |10012 |
| Bengaluru | 1 |10013 |
| Lucknow | 3 |20001 |
| Mysore | 4 |50001 |
| Mysore | 4 |50002 |
| Mysore | 4 |50003 |
| . | . | . |
预期结果:
查询应该检查已经存在的城市并过滤掉那些不应添加的城市。 (这是使用 NOT IN 完成的)
另外,如果有多个相同城市的行,则不应添加多个相同城市的副本。例如:查询应该只添加一行 City- 'Mysore' 而不是三行
我执行的查询:
SELECT
City, id_state
FROM
@table
WHERE
City NOT IN (SELECT City FROM City WHERE City IS NOT NULL)
AND id_state NOT IN (SELECT id_state FROM City WHERE id_state IS NOT NULL)
您可以使用 select 不同的:
SELECT distinct City, id_state FROM @table WHERE City
NOT IN (SELECT City FROM City WHERE City IS NOT Null) AND id_state NOT
IN (SELECT id_state FROM City WHERE id_state IS NOT Null)
一个城市名称可以属于多个州,因此您需要检查这两种组合。
我觉得下面的查询应该会给你想要的输出。
SELECT DISTINCT City, id_state
FROM @table T1
WHERE NOT EXISTS
(
SELECT 1 FROM City C WHERE C.City = T1.City AND T.id_state = C.id_state
)
考虑使用 NOT EXISTS 而不是 NOT IN:
SELECT DISTINCT City
, id_state
FROM @table t1
WHERE NOT EXISTS ( SELECT City
FROM City t2
WHERE t1.City = t2.City
AND t1.id_state = t2.id_state
);
原因:
- 一个子句可以定义多个谓词,所以需要有多个NOT IN
- 从性能的角度来看,它的查询计划比 NOT IN 更快。
您可以使用 NOT IN 和 NOT EXISTS 实现此目的
不在:
SELECT DISTINCT t.City, t.id_state
FROM @table t
WHERE City NOT IN (SELECT c.City FROM City WHERE c.city = t.city AND c.id_state = t.id_state)
不存在:
SELECT DISTINCT t.City, t.id_state
FROM @table t
WHERE NOT EXISTS (SELECT TOP 1 c.city FROM City WHERE c.city = t.city AND c.id_state = t.id_state)
NOT EXISTS 比 NOT IN 好。也不需要多个 NOT IN。
我有两个这样的 table:
Table 1: City
- 主键: ID_City
:
| City | ID_City | ID_State|
+-----------+---------+---------+
| Chennai | 1 | 2 |
| Bengaluru | 2 | 1 |
| Lucknow | 3 | 3 |
| . | . | . |
| . | . | . |
我正在向现有数据库添加新城市,同时使用 NOT IN 检查现有城市。 @table 看起来像这样:
Table 1: @table
| City | ID_State|Zip_Code|
+-----------+---------+--------+
| Chennai | 2 |00001 |
| Chennai | 2 |00002 |
| Chennai | 2 |00003 |
| Bengaluru | 1 |10011 |
| Bengaluru | 1 |10012 |
| Bengaluru | 1 |10013 |
| Lucknow | 3 |20001 |
| Mysore | 4 |50001 |
| Mysore | 4 |50002 |
| Mysore | 4 |50003 |
| . | . | . |
预期结果:
查询应该检查已经存在的城市并过滤掉那些不应添加的城市。 (这是使用 NOT IN 完成的)
另外,如果有多个相同城市的行,则不应添加多个相同城市的副本。例如:查询应该只添加一行 City- 'Mysore' 而不是三行
我执行的查询:
SELECT
City, id_state
FROM
@table
WHERE
City NOT IN (SELECT City FROM City WHERE City IS NOT NULL)
AND id_state NOT IN (SELECT id_state FROM City WHERE id_state IS NOT NULL)
您可以使用 select 不同的:
SELECT distinct City, id_state FROM @table WHERE City
NOT IN (SELECT City FROM City WHERE City IS NOT Null) AND id_state NOT
IN (SELECT id_state FROM City WHERE id_state IS NOT Null)
一个城市名称可以属于多个州,因此您需要检查这两种组合。
我觉得下面的查询应该会给你想要的输出。
SELECT DISTINCT City, id_state
FROM @table T1
WHERE NOT EXISTS
(
SELECT 1 FROM City C WHERE C.City = T1.City AND T.id_state = C.id_state
)
考虑使用 NOT EXISTS 而不是 NOT IN:
SELECT DISTINCT City
, id_state
FROM @table t1
WHERE NOT EXISTS ( SELECT City
FROM City t2
WHERE t1.City = t2.City
AND t1.id_state = t2.id_state
);
原因:
- 一个子句可以定义多个谓词,所以需要有多个NOT IN
- 从性能的角度来看,它的查询计划比 NOT IN 更快。
您可以使用 NOT IN 和 NOT EXISTS 实现此目的
不在:
SELECT DISTINCT t.City, t.id_state
FROM @table t
WHERE City NOT IN (SELECT c.City FROM City WHERE c.city = t.city AND c.id_state = t.id_state)
不存在:
SELECT DISTINCT t.City, t.id_state
FROM @table t
WHERE NOT EXISTS (SELECT TOP 1 c.city FROM City WHERE c.city = t.city AND c.id_state = t.id_state)
NOT EXISTS 比 NOT IN 好。也不需要多个 NOT IN。