表结构及数据

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

原文:https://www.cnblogs.com/xiaoyezi/p/9274443.html