Oracle SQL union及minus及intersect 綜合用法教學

使用工具sqldeveloper
  請先參考 sqldeveloper下載及安裝及連線

測試資料來源請先參考 Oracle DB 目錄


Union 對兩個結果資料做合併資料,去除重複資料
minus 留下上方結果資料,去掉重複的資料及下方結果資料
intersect 留下重複的資料

例:
使用 employees 員工表格

(表1 員工 100~105
Union 對兩個結果資料做合併資料,去除重複資料
表2 員工 105~115
)
intersect 留下重複的資料
(
表3 員工 110~118
minus 留下 上方結果資料,去掉重複的資料及下方結果資料
表4 員工 115~120
)

簡單測試語法:

(-- 100 ~ 115
SELECT employee_id,last_name,salary
FROM employees
WHERE employee_id between 100 and 105
UNION
SELECT employee_id,last_name,salary
FROM employees
WHERE employee_id between 105 and 115
)
intersect
(-- 110 ~ 114
SELECT employee_id,last_name,salary
FROM employees
WHERE employee_id between 110 and 118
minus
SELECT employee_id,last_name,salary
FROM employees
WHERE employee_id between 115 and 120
);
測試:






 

 








其它文章

arrow
arrow

    PG Levin Li 發表在 痞客邦 留言(0) 人氣()