select value from temp_a a
where a.id between 1 and 100
and not exists(select * from temp_b b where a.value=b.value);
这时能查出结果
select value from temp_a a
where a.id between 1 and 100
and a.value not in(select value from temp_b);
此时查出的结果为空.
经过google终于找出原因: 内表(temp_b)有空值. 用not in得到的结果集都为空.以下是结论:
1、对于not exists查询,内表存在空值对查询结果没有影响;对于not in查询,内表存在空值将导致最终的查询结果为空。
2、对于not exists查询,外表存在空值,存在空值的那条记录最终会输出;对于not in查询,外表存在空值,存在空值的那条记录最终将被过滤,其他数据不受影响。
因此,请尽量不要使用not in(它会调用子查询),而尽量使用not exists(它会调用关联子查询)。如果子查询中返回的任意一条记录含有空值,则查询将不返回任何记录,正如上面例子所示。
除非子查询字段有非空限制,这时可以使用not in ,并且也可以通过提示让它使用hasg_aj或merge_aj连接
浙公网安备 33010602011771号