select c1, c2 from t1
where not exists
(
select 1 from t2
where t1.c2 = t2.c3
)
Run Code Online (Sandbox Code Playgroud)
和
select c1, c2 from t1
where c2 not in
(
select c3 from t2
)
Run Code Online (Sandbox Code Playgroud)
not exists返回c2等于的记录NULLnot in并没有返回记录,其中c2等于NULLDECLARE @t1 TABLE (c1 INTEGER, c2 INTEGER)
DECLARE @t2 TABLE (c3 INTEGER)
INSERT INTO @t1 VALUES (NULL, NULL), (1, NULL), (NULL, 1), (1, 1)
INSERT INTO @t2 VALUES (NULL), (1)
select c1, c2 from @t1 t1
where not exists
(
select 1 from @t2 t2
where t1.c2 = t2.c3
) -- returns (NULL, NULL) & (1, NULL)
select c1, c2 from @t1
where c2 not in
(
select c3 from @t2
) -- returns nothing
Run Code Online (Sandbox Code Playgroud)