欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

oracle 树结构查询

程序员文章站 2022-05-06 22:21:28
...
SELECT RPAD(' ', 4 * (LEVEL-1), '--') || name as name,
       CONNECT_BY_ROOT name "ROOT",
       CONNECT_BY_ISLEAF "ISLEAF",
       LEVEL,
       t.*,
       SYS_CONNECT_BY_PATH(t.name, '/') "PATH"

  from sys_department t
START WITH T.DEPARTMENTID = 1
CONNECT BY NOCYCLE PRIOR T.Departmentid = T.PARENTDEPARTMENTID
order siblings by t.departmentid;



SELECT RPAD(' ', 2 * (LEVEL - 1), '-') || Tn_Id,
       CONNECT_BY_ROOT Tn_Id "ROOT",
       CONNECT_BY_ISLEAF "ISLEAF",
       LEVEL,
       t.*,
       SYS_CONNECT_BY_PATH(t.Tn_Id, '/') "PATH"

  from (
  (SELECT c.tn_id, c.tn_name, c.parent_id, c.orders
           FROM tree_node_c c
          where c.proj_phase = 'PD'
            and (c.view_mode = 2 or c.view_mode = 0) )
            union
        select d.tn_id, d.tn_name, d.parent_id, d.orders
           from tree_node_d d
          where d.proj_id = '15-B154C'
            AND (d.view_mode = 2 or d.view_mode = 0)
            and d.proj_phase = 'PD'

          ) t
START WITH T.Parent_Id = 'DP1_000'
CONNECT BY NOCYCLE PRIOR T.Tn_Id = T.PARENT_ID
order siblings by orders