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, exists, not exists 的区别资料很多,但基本都雷同。而且讲解的也不是那么明确,欢迎大家来讨论,根据自己的理解。以上两个例子的结果居然不同,有人知道为什么吗?  最好能探讨下它们运行的机理,给大家一个明确的认识。

解决方案 »

  1.   

    运行结果:(2,null)和(2,1)是匹配的,为什么会这样呢?我也想知道。
      

  2.   

    select 'true' from dual where (1,2) not in ((1,null),(2,3),(2,null)); 
    补充进去,结果又是如何?
      

  3.   

    NULL值的問題,NULL與任何值的匹配結果都是NULL ,
    NULL 使用详解[轉貼]NULL 使用详解问:什么是NULL?
    答:在我们不知道具体有什么数据的时候,也即未知,可以用NULL,我们称它为空,ORACLE中,含有空值的表列长度为零。
    ORACLE允许任何一种数据类型的字段为空,除了以下两种情况:
    1、        主键字段(primary key),
    2、        定义时已经加了NOT NULL限制条件的字段说明:
    1、        等价于没有任何值、是未知数。
    2、        NULL与0、空字符串、空格都不同。
    3、        对空值做加、减、乘、除等运算操作,结果仍为空。
    4、        NULL的处理使用NVL函数。
    5、        比较时使用关键字用“is null”和“is not null”。
    6、        空值不能被索引,所以查询时有些符合条件的数据可能查不出来,count(*)中,用nvl(列名,0)处理后再查。
    7、        排序时比其他数据都大(索引默认是降序排列,小→大),所以NULL值总是排在最后。
    ·        使用方法: 
    ·        SQL> select 1 from dual where null=null;
    ·        
    ·        没有查到记录
    ·        
    ·        SQL> select 1 from dual where null='';
    ·        
    ·        没有查到记录
    ·        
    ·        SQL> select 1 from dual where ''=''; 
    ·        
    ·        没有查到记录
    ·        
    ·        SQL> select 1 from dual where null is null;
    ·        
    ·                1
    ·        ---------
    ·                1
    ·        
    ·        SQL> select 1 from dual where nvl(null,0)=nvl(null,0);
    ·        
    ·                1
    ·        ---------
    ·                1
    ·        
    ·        对空值做加、减、乘、除等运算操作,结果仍为空。
    ·        SQL> select 1+null from dual;
    ·        SQL> select 1-null from dual;
    ·        SQL> select 1*null from dual;
    ·        SQL> select 1/null from dual;
    ·        
    ·        
    ·        查询到一个记录.
    ·        
    ·        注:这个记录就是SQL语句中的那个null
    ·        
    ·        设置某些列为空值
    ·        update table1 set 列1=NULL where 列1 is not null;
    ·        
    ·        
    ·        现有一个商品销售表sale,表结构为:
    ·        month    char(6)      --月份
    ·        sell    number(10,2)   --月销售金额
    ·        
    ·        create table sale (month char(6),sell number);
    ·        insert into sale values('200001',1000);
    ·        insert into sale values('200002',1100);
    ·        insert into sale values('200003',1200);
    ·        insert into sale values('200004',1300);
    ·        insert into sale values('200005',1400);
    ·        insert into sale values('200006',1500);
    ·        insert into sale values('200007',1600);
    ·        insert into sale values('200101',1100);
    ·        insert into sale values('200202',1200);
    ·        insert into sale values('200301',1300);
    ·        insert into sale values('200008',1000);
    ·        insert into sale(month) values('200009');(注意:这条记录的sell值为空)
    ·        commit;
    ·        共输入12条记录
    ·        
    ·        SQL> select * from sale where sell like '%';
    ·        
    ·        MONTH       SELL
    ·        ------ ---------
    ·        200001      1000
    ·        200002      1100
    ·        200003      1200
    ·        200004      1300
    ·        200005      1400
    ·        200006      1500
    ·        200007      1600
    ·        200101      1100
    ·        200202      1200
    ·        200301      1300
    ·        200008      1000
    ·        
    ·        查询到11记录.
    ·        
    ·        结果说明:
    ·        查询结果说明此SQL语句查询不出列值为NULL的字段
    ·        此时需对字段为NULL的情况另外处理。
    ·        SQL> select * from sale where sell like '%' or sell is null;
    ·        SQL> select * from sale where nvl(sell,0) like '%';
    ·        
    ·        MONTH       SELL
    ·        ------ ---------
    ·        200001      1000
    ·        200002      1100
    ·        200003      1200
    ·        200004      1300
    ·        200005      1400
    ·        200006      1500
    ·        200007      1600
    ·        200101      1100
    ·        200202      1200
    ·        200301      1300
    ·        200008      1000
    ·        200009
    ·        
    ·        查询到12记录.
    ·        
    ·        Oracle的空值就是这么的用法,我们最好熟悉它的约定,以防查出的结果不正确
      

  4.   

    替換一下是同樣的結果
    select 'true' from dual where (2,null) not in ((2,3),(2,1));
      

  5.   

    所有与null的操作(+,-,*,/,比较)返回的结果都是null
      

  6.   

    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,原因是第二个元素短路了第一个元素。以上分析纯属个人观点,仅供参考。