標籤: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?
- select *from student1
- union all
- select *from student2
select *from student1union allselect *from student2
查後後結果
看到測試結果就明白了,union all對兩個結果集進行並集操作,包括重複行,不進行排序。
如果去掉all 關鍵字,
[sql] view plain copy print?
- select *from student1
- union
- select *from student2
select *from student1unionselect *from student2
看到結果,得出的結論是:對兩個結果集進行並集操作,不包括重複行,同時進行預設規則的排序
2.交集
[delphi] view plain copy print?
- select *from student1
- intersect
- select *from student2
select *from student1intersectselect *from student2
結果為:
是的,返回查詢結果中相同的部分即是他們的交集
補充一下:minus 關鍵字
查詢時候把表1放在前面,
[sql] view plain copy print?
- select *from student1
- minus
- select *from student2
select *from student1minusselect *from student2
結果為:
查詢時候把表2放在前面,
[sql] view plain copy print?
- select *from student2
- minus
- select *from student1
select *from student2minusselect *from student1
結果為:
使用 minus 返回在第一個查詢結果中與第二個查詢結果不相同的那部分行記錄,即兩個結果的差集
使用以上查詢的結果集有兩個最基本的規則:
(1)所有查詢中的列數和列的順序必須相同。
(2)資料類型必須相容
oracle 交集和並集