oracle 父子关系

时间:2022-06-13 11:31:50

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

表结构及数据

oracle 父子关系

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

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

oracle 父子关系

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

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

oracle 父子关系

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

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

oracle 父子关系

4.查找孙节点

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

oracle 父子关系

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

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

oracle 父子关系