所有查询中的列数和列的顺序必须相同.
数据类型必须兼容.
并且它们都是处理于多个结果集中有重复数据的问题 首先还是创建测试环境 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 两个表的初始结果如下
select * from temptable2 select * from temptable1
union all
select * from temptable2
有 ALL 关键字是完全整合两个结果集,而无 ALL 是在之前的基础上去重了,所以第一个查询中{id:1, price:3}只会显示一条,结果如下:
except
select * from temptable2