下面我会比较 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

 SQL SERVER EXCEPT 和 INTERSECT

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

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

SQL SERVER EXCEPT 和 INTERSECT

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

相关文章:

  • 2021-11-01
  • 2021-09-01
  • 2021-11-03
  • 2021-11-02
  • 2021-11-29
  • 2020-04-23
  • 2021-08-05
  • 2018-05-15
猜你喜欢
  • 2018-03-30
  • 2019-06-03
  • 2021-11-05
  • 2021-11-02
  • 2021-11-18
  • 2021-10-05
  • 2021-08-05
相关资源
相似解决方案