SQLServer數據集合的交、並、差集運算


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*/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM