当前所在位置:珠峰网资料 >> 计算机 >> Oracle认证 >> 正文
oracle父子关系树递归排序
发布时间:2011/3/24 12:59:40 来源:城市学习网 编辑:ziteng
  数据库中常要处理父子关系的记录,在oracle中可以用查询语句一次把所有的子记录全部取出来。例如下:

  t1

  t11

  t111

  t1111

  t12

  t121

  t1211

  db数据字段如下:

  task_id task_name t.parent_task_id ***

  *** *** *** ***

  000001 t1 *** ***

  000002 t11 000001 ***

  000005 t12 000001 ***

  000003 t111 000002 ***

  000004 t1111 000003 ***

  000006 t121 000005 ***

  000007 t1211 000006 ***

  *** *** *** ***

  查询语句:

  select t.task_id ,t.task_name ,t.parent_task_id

  from t_task t

  start with task_id='000001'

  connect by prior task_id = parent_task_id;

  结果显示:

  task_id task_name t.parent_task_id

  000001 t1

  000002 t11 000001

  000003 t111 000002

  000004 t1111 000003

  000005 t12 000001

  000006 t121 000005

  000007 t1211 000006

  strat with 指定层次开始的条件,即是说满足这个条件的行即可以做为层次树的最顶层

  connect by prior指层之间的关联条件,即什么样的行是上层行的子行(自连接条件)

  select level ,id,name,parentid from temptable2

  connect by prior parentid(属于顶层的列)=id(子层的列) start with id =1

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved