码迷,mamicode.com
首页 > 其他好文 > 详细

第29章 层次查询

时间:2018-08-04 16:43:19      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:nes   smi   方法   查询   使用   span   ted   上下   nec   

第29章 层次查询

查询员工上下级关系

查看员工上级领导
select empno,ename,mgr from emp start with empno=7839 connect by prior mgr=empno;


查看领导的下属
select empno,ename,mgr from emp start with empno=7839 connect by prior empno=mgr;

分级查看,加入位列level
select empno,ename,mgr,level from emp start with empno=7839 connect by prior mgr=empno;

select empno,ename,mgr,level from emp start with empno=7839 connect by prior empno=mgr;


层次关系美化

第一种方法

select lpad(ename,(length(ename)+level*2)-2,‘ ‘) ename from emp start with empno=7839 connect by prior empno=mgr;

SQL> select lpad(ename,(length(ename)+level*2)-2,‘ ‘) ename from emp start with empno=7839 connect by prior empno=mgr;

ENAME
--------------------------------------------------------------------------------
KING
JONES
SCOTT
ADAMS
FORD
SMITH
BLAKE
ALLEN
WARD
MARTIN
TURNER

ENAME
--------------------------------------------------------------------------------
JAMES
CLARK
MILLER

14 rows selected.


第二种方法,使用系统函数sys_connect_by_path(colname,‘ ‘)查看

select sys_connect_by_path(ename,‘/‘) ename from emp start with empno=7839 connect by prior empno=mgr;

SQL> select sys_connect_by_path(ename,‘/‘) ename from emp start with empno=7839 connect by prior empno=mgr;

ENAME
--------------------------------------------------------------------------------
/KING
/KING/JONES
/KING/JONES/SCOTT
/KING/JONES/SCOTT/ADAMS
/KING/JONES/FORD
/KING/JONES/FORD/SMITH
/KING/BLAKE
/KING/BLAKE/ALLEN
/KING/BLAKE/WARD
/KING/BLAKE/MARTIN
/KING/BLAKE/TURNER

ENAME
--------------------------------------------------------------------------------
/KING/BLAKE/JAMES
/KING/CLARK
/KING/CLARK/MILLER

14 rows selected.

 

第29章 层次查询

标签:nes   smi   方法   查询   使用   span   ted   上下   nec   

原文地址:https://www.cnblogs.com/tudousix/p/9418998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!