(转)not in(1,2..) ,null 查不出来的问题

今天同事遇到一个问题:

select    'true '   from   dual   where   (1,2)   not   in   ((2,3),(2,null)); 

select 'true ' from dual where (2,1) not in ((2,3),(2,null)); 

以上两个例子的结果居然不同
 
 
in/not   in在判断NULL时用的与=/ <> 一样的方式,即必须用is   null来判断,否则始终为失败。 
语句 
select   'true '   from   dual   where   (1,2)   not   in   ((2,3),(2,null)); 
成功的原因在于判断二元值时首先判断其中一个非null元素,该元素成功或者失败会“短路”另一个个。由于上述第一个元素就使not   in成功了,因此第二个元素就不再处理。 
语句 
select   'true '   from   dual   where   (2,1)   not   in   ((2,3),(2,null)); 
的第一个元素没有造成“短路”,因此引起了null判断,从而结果始终为失败。 
请再实验语句 
select   'true '   from   dual   where   (2,1)   not   in   ((2,3),(null,3)); 
其结果为true,原因是第二个元素短路了第一个元素。 

猜你喜欢

转载自cainiao1923.iteye.com/blog/2348884