SQLServer 數(shù)據(jù)集合的交、并、差集運算
更新時間:2009年09月07日 17:52:58 作者:
SQLServer2005通過intersect,union,except和三個關(guān)鍵字對應(yīng)交、并、差三種集合運算。
他們的對應(yīng)關(guān)系可以參考下面圖示相關(guān)測試實例如下:
相關(guān)測試實例如下:
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
/*求表并集不過濾重復(fù)
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*/
相關(guān)測試實例如下:
復(fù)制代碼 代碼如下:
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
/*求表并集不過濾重復(fù)
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*/
您可能感興趣的文章:
相關(guān)文章
關(guān)于sqlserver 2005 使用臨時表的問題( Invalid object name #temptb)
最近在利用 SSRS 2005 做報表的時候,調(diào)用帶有臨時表的數(shù)據(jù)源時,系統(tǒng)會報錯,并無法進入向?qū)У南乱徊?提示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應(yīng)用
這篇文章介紹了SQL?Server中帶有OUTPUT子句的INSERT,DELETE,UPDATE應(yīng)用,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
SQL2005 學(xué)習(xí)筆記 公用表表達式(CTE)
公用表表達式是Sql Server2005新增加的一個非常好用的功能。2009-07-07

