use tempdb
go
if (object_id ('t1' ) is not null ) drop table t1
if (object_id ('t2' ) is not null ) drop table t2
go
create table t1 (a int )
insert into t1 select 1 union select 2 union select 3
create table t2 (a int )
insert into t2 select 3 union select 4 union select 5
go
select * from t1 union select * from t2
go
/* 求表合集
1
2
3
4
5*/
select * from t1 union all select * from t2
go
/*求表合集不过滤重复
1
2
3
3
4
5*/
select * from t1 except select * from t2
go
/*求t1对t2的差集
1
2*/
select * from t1 intersect select * from t2
go
/*求t1对t2的交集
3*/
本文来自优快云博客:http://blog.youkuaiyun.com/jinjazz/archive/2009/09/07/4527863.aspx
本文演示了如何使用SQL的基本集合操作,包括UNION、UNION ALL、EXCEPT和INTERSECT来处理两个表的数据。通过这些操作可以实现数据的合集、差集和交集等需求。
8075

被折叠的 条评论
为什么被折叠?



