SQLServer 数据集合的交、并、差集运算
更新时间:2009年09月07日 17:52:58 作者:
SQLServer2005通过intersect,union,except和三个关键字对应交、并、差三种集合运算。
他们的对应关系可以参考下面图示相关测试实例如下:
相关测试实例如下:
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*/
相关测试实例如下:
复制代码 代码如下:
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*/
您可能感兴趣的文章:
相关文章
关于sqlserver 2005 使用临时表的问题( Invalid object name #temptb)
最近在利用 SSRS 2005 做报表的时候,调用带有临时表的数据源时,系统会报错,并无法进入向导的下一步,提示There is an error in the query. Invalid object name #temptb2012-07-07
SQL Server 在Management Studio中使用Web浏览器
本文只是分享一个使用Management Studio的技巧,需要的朋友可以参考一下。2016-05-05
SQL Server中带有OUTPUT子句的INSERT,DELETE,UPDATE应用
这篇文章介绍了SQL Server中带有OUTPUT子句的INSERT,DELETE,UPDATE应用,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-06-06


最新评论