Oracle 外链接,自连接,层次查询

select d.deptno 部门号,d.dname 部门名称,count(e.empno) 人数
from emp e,dept d
where e.deptno(+)=d.deptno
group by d.deptno,d.dname;   --外链接。 e.deptno(+)=d.deptno表示左外连接


select e.ename 员工姓名,b.ename 老板姓名
from emp e,emp b
where e.mgr=b.empno;   --自连接。 同一张表两个别名。(自连接的缺点:笛卡尔积成指数级增长,不适合操作大表)


select level,empno,ename,mgr 
from emp
connect by prior empno=mgr
start with mgr is nul
order by 1;        --层次查询。 level其实是伪列,表示层次(深度)  (优点是不像自连接一样产生巨大的笛卡尔积;但是不能直观看出父级)

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82351387