ORACEL tree structure query

ORACEL tree structure query

I have been in the company's development project for more than a year, and it is still a bit difficult to meet a demand. After some discussion and research, I finally figured it out! Share it with fellow Taoists today, hoping to encounter such problems in research and development Can help a little bit, see the sun through the clouds and mists! Of course, these are nonsense, mainly for foreshadowing and sensational^^ Not much nonsense, let’s start with the code!

need

Query the corresponding task according to the task number

difficulty

  1. Tasks are stored in the database hierarchically according to the tree structure
  2. Requires fuzzy matching corresponding tasks
  3. multiple tabs

Technology
START WITH ... CONNECT BY PRIOR ... ORDER SIBLINGS BY ...
Here, CONNECT BY PRIOR is followed by the root node, then it is traversed from bottom to top,
if it is followed by leaf nodes, it is traversed from top to bottom
For example:
traverse from bottom to top
CONNECT BY PRIOR TT.PARENT_TASK_ID = TT.TASK_ID
traverse from top to bottom
CONNECT BY PRIOR T.TASK_ID = T.PARENT_TASK_ID
code

WITH RST AS (SELECT DISTINCT TT.*,
                             TT.TASK_LEVEL TREELEVEL
               FROM TASK_TABLE TT
              START WITH (TT.TASK_ID in ('-9999') or
                         TT.TASK_ID in
                         (模糊查询出所有匹配的ID))
             CONNECT BY PRIOR TT.PARENT_TASK_ID = TT.TASK_ID
              ORDER SIBLINGS BY TT.FIRST_ORDER, TT.SERIAL_NUMBER)
/*1.PRIOR先自底向上进行遍历,所有叶子节点及根节点WITH AS SELECT ORDER SIBLINGS BY;
		2.PRIOR再自顶向下进行遍历WITH AS SELECT ORDER SIBLINGS BY*/
SELECT T.*
  FROM RST T
 START WITH T.TASK_ID IN (SELECT TASK_ID
                                   FROM (SELECT TASK_ID, ROWNUM RN
                                           FROM RST
                                          WHERE TASK_LEVEL = '1'
                                            AND ROWNUM <= 10)
                                  WHERE RN >= 1)
CONNECT BY PRIOR T.TASK_ID = T.PARENT_TASK_ID
 ORDER SIBLINGS BY T.FIRST_ORDER, T.SERIAL_NUMBER

result set
insert image description here

Guess you like

Origin blog.csdn.net/qq_37980551/article/details/89888668