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) 

  • 相关阅读:
    移动端布局方案汇总&&原理解析
    Javascript运行机制
    git 使用
    async await详解
    vue使用axios调用豆瓣API跨域问题
    hash和history的区别
    http状态码
    XSS 和 CSRF简述及预防措施
    【pytorch】pytorch基础学习
    [源码解读] ResNet源码解读(pytorch)
  • 原文地址:https://www.cnblogs.com/itxuexiwang/p/6397469.html
Copyright © 2011-2022 走看看