oracle 父子关系

语句递归查找父子关系语句

表结构及数据

1.通过根节点遍历子节点

select t.*,LEVEL from Test2 t START WITH t.parentid=0 CONNECT BY PRIOR t.id = t.parentid

2.通过子节点向根节点追溯

select t.*,LEVEL from Test2 t START WITH t.id='13' CONNECT BY PRIOR t.parentid = t.id

3.查找直接子节点(下一层)

select t.*,LEVEL from Test2 t where LEVEL = 2 START WITH t.parentid=0 CONNECT BY PRIOR t.id = t.parentid

 

4.查找孙节点

select t.*,LEVEL from Test2 t where LEVEL = 3 START WITH t.parentid=0 CONNECT BY PRIOR t.id = t.parentid

5.查找父节点(直接上级)

select t.*,LEVEL from Test2 t where LEVEL = 2 START WITH t.id='13' CONNECT BY PRIOR t.parentid = t.id

 

posted @ 2018-07-06 16:43  木头爹  阅读(462)  评论(0编辑  收藏  举报