Architect's Oracle------------ Parent-child sequence recursive query

1. Foreword.
  As the title.

2. code.

In the database, the records of the parent-child relationship are often processed. In Oracle, you can use the query statement to fetch all the child records at one time. For example:
t1
 t11
     t111
        t1111
 t12
     t121
        t1211
 
The db data fields are as follows:
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                         ***
***                     ***                       ***                                 ***
check sentence:
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;
The results show that:
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 specifies the condition for the start of the hierarchy, that is, the row that satisfies this condition can be used as the top level of the hierarchy tree
  
connect by prior refers to the association condition between layers, that is, what kind of row is the child row of the upper layer row (self-connection condition)
 
select level ,id,name,parentid from temptable2
  connect by prior parentid (column belonging to top level) = id (column of child level) start with id =1



2. Note:
PRIOR is placed in front of the connect equal sign to search from the top level down. If it is placed after the equal sign, such as =PRIOR id, it can be searched from the bottom up, which can be used to implement a statement to find the top or bottom layer.
From: http://blog.sina.com.cn/s/blog_46d93f190101h7y6.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326565067&siteId=291194637