Oracle 集合運算

2021-08-30 12:14:48 字數 1491 閱讀 5497

使用者scott下

emp表:

select deptno from dept

[img]

emp表:

select deptno from emp

[img]

oracle集合函式包括:minus(差集),union(並集),union all(不排重並集),intersect(交集)

1.minus:找到兩個給定的數值集合之間的差異,意味著找到乙個數值集合,其中的元素僅存在於前乙個集合中,而不存在於另乙個集合中。

select deptno from dept 

minus select

deptno from emp

[img]

分析:查相同列名的資料,查出的40,只存在與前一張表,即dept表中。

2.union:連線查詢的兩張表中相同列名的資料的並集。

select deptno from dept 

union

select deptno from emp

[img]

分析:查出2張表中的並集。

3.union all:連線查詢兩張表中相同列名的所有資料。(與union 不同的是它不去除重複資料)。

select deptno from dept 

union all

select deptno from emp

[img]

分析:查出2張表中所有的值。

4.intersect:查詢intersect連線的兩張表中相同列名的資料,查出交集的資料。

select deptno from dept 

intersect

select deptno from emp

[img]

分析:查出2張表中所有的交集。

5.舉個例子說明集合運算的應用:

查詢出沒有員工的那個部門的部門編號和部門名稱.

(1).平常做法:

select deptno,dname from dept  

where deptno not in

(select distinct (deptno) from emp)

[img]

(2).使用minus做法:

select deptno,dname from dept  

where deptno in

(select deptno from dept

minus

select deptno from emp

)

[img]

結果:(一樣)

oracle集合運算

主要運用 資料統計 並集 union 交集 interset 差集 minus 使用oracle提供的scott使用者進行演示 工資大於1500 或者是20號部門下的員工 並集運算 1.使用union select from emp where sal 1500 union select from ...

Oracle 集合運算

集合運算注意的問題 union並集 intersect交集 minus差集 1 參與運算的各個集合必須列數相同 且型別一致 2 採用第乙個集合作為最後結果的表頭 3 order by永遠在最後 4 括號 sql優化 盡量不要使用集合運算 多次查詢資料庫,效率低 select from emp whe...

Oracle 集合運算

準備工作 oracle使用者scott下emp表 dept表。dept表 select t.deptno from dept t 結果 table deptno 10 20 30 40 table emp表 select t.deptno from emp t 結果 table deptno 10 ...