Oracle中的集合運算

2022-02-26 22:34:00 字數 1400 閱讀 3378

前言:在實際專案中對多表進行集合運算使用非常廣泛,以下是對集合操作的部分總結。

一,建表,插入測試資料

1

create

table

a(2 numbers integer3)

45create

table

b(6 numbers integer7)

89insert

into a values(7956

);10

insert

into a values(7953

);11

insert

into a values(7966

);12

insert

into a values(7933

);13

commit

1415

insert

into b values(7979

);16

insert

into b values(7955

);17

insert

into b values(7968

);18

insert

into b values(7933

);19

commit

二,求並集,返回a,b兩表中所有記錄,[去重]

1

select numbers froma2

union

3select numbers from b

三,求並集,返回a,b兩表中所有記錄,[不去重]

1

select numbers froma2

union

all3

select numbers from a

四,求差集,返回只存在a表中的記錄,剔除a表與b表相同的記錄

1

--方法一

2select numbers from a where numbers not

in (select numbers from

b )3

--方法二

4select numbers froma 5

minus

6select numbers from b

五,求交集,返回a表與b表中相同的記錄

1

--方法一

2select numbers from a where numbers in (select numbers from

b )3

--方法二

4select numbers froma 5

intersect

6select numbers from b

Oracle的集合運算

一 所謂的集合運算 集合運算是用來把兩個或多個查詢的結果集做並 交 查的集合運算,包含集合運算的查詢稱為復合查詢。二 集合運算的幾種方式 1 聯合運算 union 去重 不排序 聯合運算是從兩個查詢返回除去重複值後的結果。2 完全聯合運算 union all 不去重 公升序 完全聯合運算是從每個查詢...

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...