SQL SERVER: 合并相关操作(Union,Except,Intersect)
use tempdbcreate 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 2select * from temptable1select * from temptable2--union会删除重复值,也就是说A和B中重复的行,最终只会出现一次,而union all则会保留重复行。select * from temptable1unionselect * from temptable2select * from temptable1union allselect * from temptable2--差异(Except)--就是两个集中不重复的部分。例如SELECT * from temptable1exceptselect * from temptable2--交集(intersect)--就是两个集中共同的部分。例如select * from temptable1INTERSECTselect * from temptable2DROP TABLE temptable1DROP TABLE temptable2