ORACLE SQL 递归、分级查询

第十九章:分级查询

select level,ename

from emp

start with ename='KING'

connect by prior empno=mgr;


start with 子句决定了爬树的起点

connect by prior 主键=外键 子句决定了爬树的方向:

主键在前是从上向下爬(反之亦反)


col ename for a30

select level,lpad(ename,length(ename)+level*2-2,' ') ename

from emp

start with ename='KING'

connect by prior empno=mgr;


树状结构平铺:

select sys_connect_by_path(ename,'/') ename

from emp

start with ename='KING'

connect by prior empno=mgr

and ename!='BLAKE';


剪枝:

剪枝条件出现在where子句,剪一个节点

select level,lpad(ename,length(ename)+level*2-2,' ') ename

from emp

where ename!='BLAKE'

start with ename='KING'

connect by prior empno=mgr;


剪枝条件出现在connect by prior子句,剪一个分支

select level,lpad(ename,length(ename)+level*2-2,' ') ename

from emp

start with ename='KING'

connect by prior empno=mgr

and ename!='BLAKE';


你可能感兴趣的:(oracle,递归,分级)