Oracle系列:(15)集合查詢

來源:互聯網
上載者:User

標籤:oracle


使用並集運算,查詢20號部門或30號部門的員工資訊

select * from emp where deptno = 20unionselect * from emp where deptno = 30;

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/86/FD/wKiom1fQHJqCkdMGAAA6IAD2YSM221.png" title="001.PNG" alt="wKiom1fQHJqCkdMGAAA6IAD2YSM221.png" />

注意:

union:二個集合中,如果都有相同的,取其一

union all:二個集合中,如果都有相同的,都取


使用set time/timing on,開啟時間的開關

set time on;set time off;

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/86/FB/wKioL1fQHbKg1UxHAAAO-IqOO78132.png" title="002.PNG" alt="wKioL1fQHbKg1UxHAAAO-IqOO78132.png" />


使用set time/timing off,關閉時間的開關

set timing on;set timint off;

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/86/FD/wKiom1fQHgnzJP96AAAXTGalaG8288.png" title="003.PNG" alt="wKiom1fQHgnzJP96AAAXTGalaG8288.png" />


使用交集運算[intersect],查詢工資在1000-2000和1500-2500之間的員工資訊(方式一)

select * from emp where sal between 1000 and 2000intersectselect * from emp where sal between 1500 and 2500;

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/86/FB/wKioL1fQHlSzdTaSAAAby0yS9dE587.png" title="004.PNG" alt="wKioL1fQHlSzdTaSAAAby0yS9dE587.png" />


用where行過濾,查詢工資在1000-2000和1500-2500之間的員工資訊(方式二)

select * from empwhere (sal between 1000 and 2000) and (sal between 1500 and 2500);


使用差集運算[minus],查詢工資在1000-2000,但不在1500-2500之間的員工資訊(方式一)

select * from emp where sal between 1000 and 2000minusselect * from emp where sal between 1500 and 2500;

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/86/FB/wKioL1fQHpvwgBI9AAAlb0Re2nY619.png" title="005.PNG" alt="wKioL1fQHpvwgBI9AAAlb0Re2nY619.png" />


使用where行過濾,查詢工資在1000-2000,但不在1500-2500之間的員工資訊(方式二)

select * from emp where (sal between 1000 and 2000) and (sal not between 1500 and 2500);


集合查詢的細節

1)集合操作時,必須確保集合列數是相等的

select empno,ename,sal,comm from emp where deptno = 20

union

select empno,ename,sal from emp where deptno = 30;錯


2)集合操作時,必須確保集合列類型對應相同

select empno,ename,sal,comm from emp where deptno = 20

union

select empno,ename,sal,hiredate from emp where deptno = 30;錯


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)當多個集合操作時,結果的列名由第一個集合列名決定

select empno "編號",ename "姓名",sal "薪水" from emp where deptno = 20unionselect empno,ename,sal from emp where deptno = 10;

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/86/FB/wKioL1fQHyeRyj2yAAAix3UZ__A216.png" title="006.PNG" alt="wKioL1fQHyeRyj2yAAAix3UZ__A216.png" />


當多表查詢,子查詢,集合查詢都能完成同樣任務時,按如下最佳化方案選擇:

多表查詢->子查詢->集合查詢








Oracle系列:(15)集合查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.