zoukankan      html  css  js  c++  java
  • oracle intersect 取交集

       如果要从一个表中查询另一个表不存在的记录,这样的情况从不同数据的角度去分析,有些数据库提供了差集的函数,有些没有要写子查询。

    1、DB2、postgreSQL差集函数except
    select deptno from emp
    except
    select deptno from dept

    2、Oracle的差集函数minus
    select deptno from emp
    minus
    select deptno from dept

    3、Mysql和sqlserver不支持差集函数,使用子查询
    select deptno from dept where deptno not in (select deptno from emp)

    但是使用not in的时候一定要注意null值的问题,in 和not in操作里面相当于or操作。
    如果
       select deptno from dept where deptno not in (10,20,null)
    这样等效于
       select deptno from dept where deptno not (deptno =10 or deptno = 20 or deptno =null)

    not (deptno =10 or deptno = 20 or deptno =null) = (false or false or null)=(false or null)

    在SQL中,TRUE or null的结果是true,而false or null的结果是null,所以在使用in谓词和or计算时,值可能是null的情况,这一点要记住。
    要解决这样问题,可以使用not Exists和相关子查询
    select deptno from dept d where not exists(select 'xx' from emp e where e.deptno = d.deptno) 

  • 相关阅读:
    Kafka 核心 API ==> AdminClient
    Kafka ==> 简介
    设计模式之 ==> 代理设计模式
    设计模式之 ==> 工厂设计模式
    设计模式之 ==> 模板设计模式
    设计模式之 ==> 单例模式
    Linux目录【持续更新中】
    Python 目录【持续更新中】
    kafka-eagle部署
    ES集群部署
  • 原文地址:https://www.cnblogs.com/itxuexiwang/p/6397469.html
Copyright © 2011-2022 走看看