当连接列或比较列有null空值,用 not in 和 <> all 表达式会过滤掉连接列或比较列值为null的记录(虽然连接列或比较列为null,但这条记录的其它列是非空的),这样可能就取不了自己本意想要的记录,用 not exists 表达式可以规避这个问题,如下:
04:21:56 SYS@orcl*SQL > select * from t1;
COL1 CO
-------------------- --
1 A
2 B
3 C
4
Elapsed: 00:00:00.01
04:21:59 SYS@orcl*SQL > select * from t2;
CO COL3
-- ----
A A2
B B2
D D2
Elapsed: 00:00:00.01
04:22:02 SYS@orcl*SQL > select * from t1 where col2 not in (select col2 from t2);
COL1 CO
-------------------- --
3 C
Elapsed: 00:00:00.01
04:22:08 SYS@orcl*SQL > select * from t1 where col2 <> any (select col2 from t2);
COL1 CO
-------------------- --
1 A
2 B
3 C
Elapsed: 00:00:00.01
04:22:14 SYS@orcl*SQL > select * from t1 where not exists (select 1 from t2 where col2=t1.col2);
COL1 CO
-------------------- --
3 C
4