oracle 递归查询父子关系记录
数据库中常要处理父子关系的记录,在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