首頁  >  文章  >  php教程  >  Oracle基礎學習之子查詢

Oracle基礎學習之子查詢

高洛峰
高洛峰原創
2017-01-06 10:37:121345瀏覽

首先使用子查詢的時候注意事項包括,子查詢可以嵌套多層和子查詢需要圓括號()括起來,下面我們來看看詳細的介紹。

基礎介紹

1,wherer:子查詢一般會傳回單行單行多列多行單列;

2,having:子查詢會傳回單行單列,同時表示要使用統計函數;

3,from:子查詢傳回多行多列資料(表格結構);

4,select:傳回單行單列(一般不使用);

範例詳解

where(進行資料行的篩選運算):

a:查詢出出低於公司平均薪資的員工資料。

select * from emp where sal<(select avg(sal) from emp);

以上的查詢返回單行單列可以作為where子句的過濾條件使用;

b:查詢公司最早僱用的僱員的資訊。

select * from emp where hiredate= (select MIN(hiredate) from emp);

C:查詢與scott從事同一工作且薪資相同的員工資訊。

select* from emp
 
where (job,sal) =( select job,sal
 
from emp
 
where ename =&#39;scott&#39;) and ename <>&#39;scott&#39;;

in:指的是與子查詢​​回傳的內容相同。

select * from emp where sal in (select sal from emp where job = &#39;manager&#39;);

not in:

select* from emp where sal not in(select sal from emp where job=&#39;manager&#39;);

子查詢中不能有空。

any:

select* from emp where sal = any(select sal from emp where job=&#39;manager&#39;);
 
select* from emp where sal > any(select sal from emp where job=&#39;manager&#39;);

比子查詢的返回的最大值要大

select* from emp where sal < any(select sal from emp where job=&#39;manager&#39;);

比子查詢返回的最大值要小

all:

all :比子查詢的回報的最大值大

     where子查詢的幾率很高;

having:

查詢出高於公司平均工資的職位名稱職位人數平均工資。

select job,count(empno),avg(sal) from emp group by job 
having avg(sal)>(select avg(sal) from emp);

select(一般不用):

查詢每位員工的編號姓名 職位 部門名稱。

select e.empno,e.ename,e.job,
 
(select d.dname from dept d whered.deptno=e.deptno)from emp e;

(1+n) 次查詢;

from(重點):

查詢出每個部門的名稱 位置 部門人數。

select d.dname,d.loc,count(e.empno)
 
from emp e,dept d
 
where e.deptno(+)=d.deptno
 
group by d.dname,d.loc;

(多表查詢)

分步1: select d.deptno,d.dname,d.locfrom dept d;
 
 分步2:select deptno,count(empno)from emp group by deptno;
 
 
 
正确的查询:
 
select d.deptno,d.dname,d.loc,temp.count
from dept d,(select deptno,count(empno) count from emp
group by deptno) temp
where d.deptno=temp.deptno(+);

多表查詢和子查詢都能實現統計,那麼那種方式更好呢?

答:在實際的工作當中,子查詢的主要目地是解決多表查詢的效能問題,所以在開發中使用的是 最多的。最大作用是解決多表查詢所帶來的笛卡爾積影響效能的問題。

複雜查詢= 簡單查詢+限定查詢+ 多表格查詢+ 分組統計查詢+子查詢;

總結

以上就是關於Oracle子查詢的全部內容,希望本文的內容對大家學習或使用Oracle能有所幫助,如果有疑問大家可以留言交流。

更多Oracle基礎學習之子查詢相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn