源动力

程序在于积累和思考
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

oracle中单表树形数据的展示

Posted on 2011-04-13 12:55  老K的幸福生活  阅读(828)  评论(0编辑  收藏  举报

在oracle中的单表树形数据,我们可以从上往下展示树形结构,也可以从下往上地展示树形结构。

connect by prior t.parent_dept_guid=t.dept_guid:可以理解为本条记录的父ID字段是下一条记录的ID字段,即由下往上结构;

connect by prior a.dept_guid=a.parent_dept_guid:可以理解为本条记录的ID字段是下一条记录的父ID字段,即由上往下结构;

select a.dept_guid,a.parent_dept_guid,(lpad(' ',to_number(level-1)*2))||dept_name dept_name,a.sub_level,level tree_Level from
(
  select t.dept_guid,t.parent_dept_guid,t.dept_name,level sub_level from tb_org_dept t
  start with t.dept_guid='321468cb-04cc-4c38-9b40-a4ca7a7d701e' connect by prior t.parent_dept_guid=t.dept_guid
) a start with a.parent_dept_guid is null connect by prior a.dept_guid=a.parent_dept_guid;