start with connect by prior recursive query usage

This clause is mainly used for recursive data query of B-tree structure type. Given any node in the B-tree structure type, traverse its final parent node or child node.

First look at the original data:

copy code
 1 create table a_test
 2 ( parentid varchar2(10),
 3   subid    varchar2(10));
 4 
 5 insert into a_test values ( '1', '2' );
 6 insert into a_test values ( '1', '3' );
 7 insert into a_test values ( '2', '4' );
 8 insert into a_test values ( '2', '5' );
 9 insert into a_test values ( '3', '6' );
10 insert into a_test values ( '3', '7' );
11 insert into a_test values ( '5', '8' );
12 insert into a_test values ( '5', '9' );
13 insert into a_test values ( '7', '10' );
14 insert into a_test values ( '7', '11' );
15 insert into a_test values ( '10', '12' );
16 insert into a_test values ( '10', '13' );
17 
18 commit;
19 
20 select * from a_test;
copy code

 

The corresponding B-tree structure is:

 


 

Let's look at an example:

Ask for the value of one of the nodes and find its final parent node. Take 7 as an example, look at the code

start with clause: There is a trick to traverse the starting conditions. If you want to check the parent node, you can use the column of the child node here, and vice versa.

connect by clause: connect condition. The keyword priority, prior is put together with the parent node column parentid, that is, traversing in the direction of the parent node; prior is put together with the sub-node column subid, it is traversing in the direction of the leaf node,

                         It doesn't matter which column parentid and subid are placed before "=", the key is who the priority is with.

order by clause: sorting, needless to say.

--------------------------------------------------

Let's take a look at an example of traversing to a leaf node:

Here, the start with clause uses the parentid column, and the specific difference is explained later with an example.

In the connect by clause, the priority is on the same side as the subid, that is, it traverses in the direction of the leaf node. Because 7 has two children, there are two results in the first level (10 and 11), 10 has two children (12, 13), and 11 has none, so the second level also has two results (12, 13 ). That is, 12,13 are leaf nodes.

 





 

 

Let's take a look at the difference between selecting different columns in the start with clause:

Take querying leaf nodes (traversing down) as an example

The result is obvious. The original intention is to take 7 as the parent node and traverse its child nodes. The left image takes the value of the parent node column, and the result is in line with the original meaning; the right image takes the value of the child node column, and the result is redundant. which shows the parent node 3 of 7.

---------------------------------------

Statements about where conditions will be recorded after verification. leave a question

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326023086&siteId=291194637