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*/
您可能感兴趣的文章:
相关文章
SQLServer2005与SQLServer2008数据库同步图文教程
要实现SQLServer2005与2005的数据库同步的话,直接用镜像就可以实现。但是如果同步 SQLServer2008的话,2005的实例是连接不上08的。低版本的无法连接高版本的。所以我们可以通过复制的方式,也就是所谓的订阅发布的方法来实现两个不同版本数据库的数据同步。2011-09-09Win2008中安装的MSSQL2005后无法访问的解决方法
最近笔者一直在使用Win2008系统,不过发现一个很奇怪的问题,那就是在该系统上安装了SQL2005后,再在其他计算机访问该主机显示不能访问2014-07-07SQL Server 2005 Express 安装失败解决办法
本人重装vs2005后,sql sever 2005 express却一直安装不上,造成写好的网页无法运行。多次卸载重装无果2009-03-03
最新评论