一、示例的表结构

create table sys_resource(id varchar2(32),pid varchar2(32),module_name varchar2(100));

...

二、解决方案说明

oracle树查询的最重要的就是select…start with…connect by…prior语法了。
#查找一个节点的所有直属子节点(所有后代)
select t.id,t.pid,t.module_name from sys_resource t start with t.id='fdb0fd0e-414b-4998-a31'connect by t.pid=prior t.id;

三、业务场景及目标脚本输出

sys_resource -项目资源模块表

系统4级菜单如下:

 

 

输出脚本如下:

with resource_tree as(
select t.id,t.pid,t.module_name,level l from sys_resource t start with t.id='fdb0fd0e-414b-4998-a31'connect by t.pid=prior t.id
)
select t1.module_name n1,t2.module_name n2,t3.module_name n3,t4.module_name n4
from resource_tree t1
join resource_tree t2 on t2.pid=t1.id
join resource_tree t3 on t3.pid=t2.id
join resource_tree t4 on t4.pid=t3.id
where t2.l=2 and t3.l=3 and t4.l=4
order by n2,n3;

 参考结果: