zoukankan      html  css  js  c++  java
  • SQL SERVER EXCEPT 和 INTERSECT

    下面我会比较 EXCEPT/INTERSECT跟 not in/in的区别,其实最主要的区别就是EXCEPT/INTERSECT可以去重,相当于 not in/in加了distinct关键字,这点类似于union和union all

    1、创建测试数据:

    create table #tempTable1 (id int , price int)
    create table #tempTable2 (id int , price int)
    insert into #tempTable1 select 3,1 union all select 3,1 union all select 1,1 union all select 1,1 union all select 1,2 
    insert into #tempTable2 select 1,1 union all select 1,1 union all select 2,1 union all select 1,5

    2、单列和所有列比对

    --所有列对比
    select * from #temptable1
    except 
    select * from #temptable2
    --只比对ID这一列
    select ID from #temptable1
    except
    select ID from #temptable2

     

    3、跟NOT IN比较:将重复值去掉了

    select ID from #temptable1
    except
    select ID from #temptable2
    ---------------------
    select ID from #temptable1
    WHERE ID NOT IN(select ID from #temptable2)

    4、INTERSECT跟in语句也是相似的,只是INTERSECT会去重。

  • 相关阅读:
    单表查询
    阻塞非阻塞同步异步&异步回调
    基于协程的TCP并发编程
    协程
    死锁与递归锁
    线程池和进程池
    GIL全局解释器锁
    数据库——多表关系
    常用数据类型与约束
    Python基础(目录)
  • 原文地址:https://www.cnblogs.com/ziqiumeng/p/10303246.html
Copyright © 2011-2022 走看看