oracle 交集和並集

來源:互聯網
上載者:User

標籤:http   技術分享   images   返回   sel   phi   ati   static   預設   

今天研究了一下oracle 交集和並集,下面把我經過尋找資料,測試後,整理如下:

1.並集

 

表1:

insert into student1 values(1,‘學生1‘);
insert into student1 values(1,‘學生2‘);
insert into student1 values(1,‘學生3‘);

 

表2:

insert into student2 values(1,‘學生1‘);
insert into student2 values(1,‘學生4‘);
insert into student2 values(1,‘學生5‘);

 

並集語句:

 

[sql] view plain copy print?
  1. select *from student1  
  2. union all  
  3. select *from student2  
select *from student1union allselect *from student2


查後後結果

 

看到測試結果就明白了,union all對兩個結果集進行並集操作,包括重複行,不進行排序。

如果去掉all 關鍵字,

 

[sql] view plain copy print?
  1. select *from student1  
  2. union  
  3. select *from student2  
select *from student1unionselect *from student2


 

看到結果,得出的結論是:對兩個結果集進行並集操作,不包括重複行,同時進行預設規則的排序

 

 

2.交集

 

[delphi] view plain copy print?
  1. select *from student1  
  2. intersect  
  3. select *from student2  
select *from student1intersectselect *from student2

 

 

結果為:

 

是的,返回查詢結果中相同的部分即是他們的交集

 

補充一下:minus 關鍵字

查詢時候把表1放在前面,

 

[sql] view plain copy print?
  1. select *from student1  
  2. minus  
  3. select *from student2  
select *from student1minusselect *from student2

 

結果為:

查詢時候把表2放在前面,

 

[sql] view plain copy print?
  1. select *from student2  
  2. minus  
  3. select *from student1  
select *from student2minusselect *from student1


結果為:

 

 

使用 minus  返回在第一個查詢結果中與第二個查詢結果不相同的那部分行記錄,即兩個結果的差集

 

 

 

 

使用以上查詢的結果集有兩個最基本的規則:
(1)所有查詢中的列數和列的順序必須相同。
(2)資料類型必須相容

 

 

oracle 交集和並集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.