SQL中union, EXCEPT 和 INTERSECT使用方法,unionintersect
这三个放在一起是有理由的,因为他们都是操作两个或多个结果集,并且这些结果集有如下限制:所有查询中的列数和列的顺序必须相同.
数据类型必须兼容.
并且它们都是处理于多个结果集中有重复数据的问题 首先还是创建测试环境 use tempdb create table tempTable1 (id int primary key identity, price int)
create table tempTable2 (id int primary key identity, price int)
insert into tempTable1 select 3 union all select 1 union all select 2 union all select 3
insert into tempTable2 select 3 union all select 4 union all select 1 union all select 2 select * from temptable1
select * from temptable2 两个表的初始结果如下
union
select * from temptable2 select * from temptable1
union all
select * from temptable2
有 ALL 关键字是完全整合两个结果集,而无 ALL 是在之前的基础上去重了,所以第一个查询中{id:1, price:3}只会显示一条,结果如下:
except
select * from temptable2
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。