`
liulanghan110
  • 浏览: 1072471 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

not in和not exists的区别

阅读更多
先创建测试数据:
 
create table test1(id number);
create table test2(id number);
insert into test1 values(1);
insert into test1 values(3);
insert into test1 values(4);
insert into test1 values(null);
insert into test1 values(null);
insert into test1 values(1);
insert into test1 values(3);
insert into test1 values(5);
insert into test1 values(null); 
insert into test1 values(null);
 
看如下SQL执行结果:
 
select  *  from test1 t1 where t1.id not in (select id from test2 t2);
 
select  *  from test1 t1 where  not exists (select 1 from test2 t2 where t2.id = t1.id);
 
第一个结果为空,第二个结果为
 
    null
 
    null
 
    6
 
两个结果不同,原因是not in里的值如果有null值会出现意想不到的结果。
 
看如下SQL:
 
    select   *  from test 1 where id not in (1,null);
 
这个SQL相当于这样:
 
    select   *  from test 1 where id  <>1 and id <>null;
 
我们知道null和所有值比较都为false,所以id <> null一直为false,导致结果为null。
 
但是in里条件有null不受影响,因为看如下SQL:
 
    select   *  from test 1 where id in (1,null);
 
这个SQL相当于这样:
 
    select   *  from test 1 where id i= 1 or id = null;
 
由于id = null一直为false,所以上面的SQL变成这样。
 
    select   *  from test 1 where id i= 1
 
即in中有null无null,效果一样
 
 
再看not exists,SQL如下:
 
select  *  from test1 t1 where  not exists (select 1 from test2 t2 where t2.id = t1.id);
 
相当于把test1表的每条记录带入后面的表达式,看是否有记录返回。
 
                                                                    
1   select 1 from test2 t2 where t2.id = 1            true   
3   select 1 from test2 t2 where t2.id = 1            true
4   select 1 from test2 t2 where t2.id = 1            false
null  select 1 from test2 t2 where t2.id = null    false
null  select 1 from test2 t2 where t2.id = null     false
 
由于是not exists,加了个not ,就变成了false  false true true true
 
所以最后的结果为
 
4
 
null 
 
null
 
所以not exists包含了结果为null的记录。
 
 
可知:
 
        not in 中包含null时,结果会为空。
 
        not in 外面包含null时,null会被剔除。
 
        not exists外面包含null时,为null的结果也会变显示。
 
上面的两条SQL,要写成一样应该这样:
 
        select  t1.id  from test1 t1 where  not exists (select 1 from test2 t2 where t2.id = t1.id) and t1.id is not null;
        select  t1.id  from test1 t1 where   t1.id not in  (select t2.id from test2 t2 where t2.id is not null);
 
 
 
 
分享到:
评论

相关推荐

    sql in,exists,not in,not exists区别

    IN、EXISTS、NOT IN、NOT EXISTS 是 SQL 中四种常用的条件判断运算符,它们之间的区别主要体现在使用场景、执行效率和语法结构上。 IN IN 是一种条件判断运算符,用于判断某个值是否存在于一个列表中。其基本语法...

    sql case when exists not exists in not in

    在SQL查询中,`CASE WHEN`、`EXISTS`、`NOT EXISTS`以及`IN`和`NOT IN`是常用的操作符,它们用于处理复杂的条件判断和数据筛选。这些概念对于理解和编写高效的SQL语句至关重要,尤其是在数据分析和数据库管理中。 `...

    sql语句优化之用EXISTS替代IN、用NOT EXISTS替代NOT IN的语句

    在查询中,我们可以使用EXISTS和NOT EXISTS来代替IN和NOT IN。例如,我们要查询Sendorder表中的冗余数据(没有和reg_person或worksite相连的数据): ```sql select Sendorder.id, Sendorder.reads, Sendorder....

    SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句.txt

    SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句.txt欢迎下载!

    经典SQL查询总结关于Exists,not Exists.in ,not in效率的说明。

    - 在某些情况下,可以考虑使用 `EXISTS` 或 `NOT EXISTS` 来替代 `IN` 和 `NOT IN`,以提高查询效率。 #### 三、左连接、右连接与全连接 除了上述几种查询方式之外,SQL 还提供了不同的连接类型来处理不同情况下...

    EXISTS_和_NOT_EXISTS

    - 效率:在某些情况下,`EXISTS` 和 `NOT EXISTS` 可能会提供相似的性能,但通常 `IN` 和 `NOT IN` 比较操作符在处理大量数据时更有效。然而,当涉及到子查询时,`EXISTS` 和 `NOT EXISTS` 可能更合适,因为它们只...

    NOT EXISTS ⇔ NOT IN ⇒ NOT NULL 问题 结果不一样 疑问 如果等效

    通过以上分析可以看出,虽然 `NOT EXISTS`、`NOT IN` 和 `NOT NULL` 在表面上看似相似,但它们之间存在着明显的区别,尤其是在处理 `NULL` 值时。因此,在实际应用中,应根据具体需求选择合适的操作符以达到最佳效果...

    sql exists和not exists用法

    这是因为`IN`和`NOT IN`需要将主查询中的每一行都与子查询结果进行比较,而`EXISTS`和`NOT EXISTS`则只需判断子查询是否返回至少一行数据即可,无需获取所有数据行。 **性能对比:** - **IN 和 NOT IN**:需要将主...

    Oracle In和exists not in和not exists的比较分析

    在Oracle数据库中,`IN`、`EXISTS`、`NOT IN` 和 `NOT EXISTS` 是四个常用的子查询操作符,它们在SQL查询语句中扮演着不同的角色,且各有其性能特点。以下是对这些操作符的详细分析和比较。 1. `IN` 操作符: `IN` ...

    一次SQL Tuning引出来的not in , not exists 语句的N种写法2

    【描述】:文章并未直接提供描述,但从标题推测,该内容可能涉及数据库查询优化,特别是关于`not in`和`not exists`两种SQL查询子句的不同使用方式和性能比较。 【标签】:SQL优化,查询性能,数据库管理 【正文】...

    MySQL中的in,exists,not in,not exists查询过程对比及结论

    MySQL优化之in,exists,not in,not exists的区别in与existsin查询过程结论:exists查询过程:结论:not in与not existsnot in查询过程:结论:not exists查询过程:结论: 首先我们使用两个用户表作为实例 insert ...

    in exist not_in

    IN、EXISTS、NOT EXISTS、NOT IN 在 SQL 语句中的应用和区别 IN 语句和 EXISTS 语句都是 SQL 语句中用来判断是否存在某个值的语句,但是它们的实现机制和应用场景是不同的。 IN 语句是通过 hash 连接来实现的,它...

    SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句2.txt

    SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句2.txt,欢迎下载!

    exists SQL用法详解、exists和not exists的常用示例

    `EXISTS`和`IN`的区别在于,`IN`通常用于比较单个字段值,而`EXISTS`可以处理更复杂的查询,它关心的是子查询是否返回行,而不关心返回的具体值。`IN`通常用于获取特定值列表内的记录,而`EXISTS`则可以用于检查记录...

    mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录

    MySQL中的`NOT IN`, `LEFT JOIN`, `IS NULL`, 和 `NOT EXISTS` 是四种不同的SQL查询方式,它们在特定情况下可以实现相似的功能,但实际执行效率可能会有很大差异。本文主要探讨这四种方法在处理大数据量时的性能表现...

    sql not in 与not exists使用中的细微差别

    not exists的子查询,对于子查询不返回行和子查询返回行的查询结果是有区别的 这些细小的差别千万不要被我们所忽视,一旦项目庞大了,想跟踪到具体的错误所花费的时间也是可观的。尽量把这些不必要的错误扼杀在摇篮...

    IN&amp;EXISTS与NOT IN&amp;NOT EXISTS 的优化原则小结

    在SQL查询中,`IN`、`EXISTS`、`NOT IN`以及`NOT EXISTS`是四个常用的比较和过滤操作符。它们在处理数据时有不同的效率和适用场景,特别是涉及到大数据量时,优化这些操作符的使用对于提升数据库查询性能至关重要。 ...

    INEXISTS与NOT INNOT EXISTS 的优化原则小结

    在SQL查询优化中,`IN`、`EXISTS`、`NOT IN`和`NOT EXISTS`是四个常见的比较和过滤条件,它们在不同场景下有不同的性能表现。以下是对这些操作符优化原则的详细说明: 1. **EXISTS的执行流程**: `EXISTS` 子查询...

Global site tag (gtag.js) - Google Analytics