Simple tree query (recursive query) in oracle, usage of PRIOR and CONNECT_BY_ROOT

1. There are 5 layers of tree data in the following style. Now we need to filter the base layer and all data below the base layer.


select *
  from AUTH_ORGANIZATION_TB t
 start with t.org_id = 32
connect by prior t.org_id = t.org_id_superior
 order by t.org_id_superior,org_id
The running results are as follows:

2. As shown in the picture above, it is known that the production technology department needs to obtain its upper two levels of data, the refining operation part

select level,
       T.ORG_ID,
       T.ORG_ID_SUPERIOR,
       RPAD('_', (level - 1) * 6, '_') || T.U_NAME,
       CONNECT_BY_ROOT ORG_ID,
       CONNECT_BY_ROOT U_NAME_FULL,
       CONNECT_BY_ISLEAF
  from AUTH_ORGANIZATION_TB T
 start with T.ORG_ID_SUPERIOR = 32
connect by prior T.ORG_ID = T.ORG_ID_SUPERIOR

The running results are as follows:


Guess you like

Origin blog.csdn.net/yerongtao/article/details/70905537