共計(jì) 1587 個(gè)字符,預(yù)計(jì)需要花費(fèi) 4 分鐘才能閱讀完成。
這篇文章主要介紹了 Oracle 中集合查詢的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓丸趣 TV 小編帶著大家一起了解一下。
使用并集運(yùn)算,查詢 20 號(hào)部門或 30 號(hào)部門的員工信息
select * from emp where deptno = 20
union
select * from emp where deptno = 30;
注意:
union:二個(gè)集合中,如果都有相同的,取其一
union all:二個(gè)集合中,如果都有相同的,都取
使用 set time/timing on,打開時(shí)間的開關(guān)
set time on;
set time off;
使用 set time/timing off,關(guān)閉時(shí)間的開關(guān)
set timing on;
set timint off;
使用交集運(yùn)算 [intersect],查詢工資在 1000-2000 和 1500-2500 之間的員工信息(方式一)
select * from emp where sal between 1000 and 2000
intersect
select * from emp where sal between 1500 and 2500;
用 where 行過(guò)濾,查詢工資在 1000-2000 和 1500-2500 之間的員工信息(方式二)
select *
from emp
where (sal between 1000 and 2000) and (sal between 1500 and 2500);
使用差集運(yùn)算 [minus],查詢工資在 1000-2000,但不在 1500-2500 之間的員工信息(方式一)
select * from emp where sal between 1000 and 2000
minus
select * from emp where sal between 1500 and 2500;
使用 where 行過(guò)濾,查詢工資在 1000-2000,但不在 1500-2500 之間的員工信息(方式二)
select *
from emp
where (sal between 1000 and 2000) and (sal not between 1500 and 2500);
集合查詢的細(xì)節(jié):
1)集合操作時(shí),必須確保集合列數(shù)是相等的
select empno,ename,sal,comm from emp where deptno = 20
union
select empno,ename,sal from emp where deptno = 30; 錯(cuò)
2)集合操作時(shí),必須確保集合列類型對(duì)應(yīng)相同
select empno,ename,sal,comm from emp where deptno = 20
union
select empno,ename,sal,hiredate from emp where deptno = 30; 錯(cuò)
3)A union B union C = C union B union A
select * from emp where deptno = 10
union
select * from emp where deptno = 20
union
select * from emp where deptno = 30;
4)當(dāng)多個(gè)集合操作時(shí),結(jié)果的列名由第一個(gè)集合列名決定
select empno 編號(hào) ,ename 姓名 ,sal 薪水 from emp where deptno = 20
union
select empno,ename,sal from emp where deptno = 10;
當(dāng)多表查詢,子查詢,集合查詢都能完成同樣任務(wù)時(shí),按如下優(yōu)化方案選擇:
多表查詢 - 子查詢 - 集合查詢
感謝你能夠認(rèn)真閱讀完這篇文章,希望丸趣 TV 小編分享的“Oracle 中集合查詢的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持丸趣 TV,關(guān)注丸趣 TV 行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!