1. Basic grammar
. 1 SELECT ... from TableName 2 Start with the condition. 1 3 Connect by Condition 2 . 4 WHERE condition 3;
E.g:
1 select * from table 2 start with org_id = ‘HBHqfWGWPy’ 3 connect by prior org_id = parent_id;
Briefly a tree structure is stored in a table, such as a table, there are two fields: org_id, parent_id, then the parent who is represented by each record is a tree structure can be formed using the above query syntax tree can get all the records, in which:
Condition 2 is connected condition, by which a recording PRIOR expressed , such as the CONNECT BY PRIOR org_id = parent_id ; means that on a record org_id parent_id section is recorded , i.e., the father of this record is the record.
Condition 3 is a filter condition for all records returned by filtration.
when scanning the tree structure table, so each node needs to access a tree structure, a node can only be accessed once, the step of accessing the following:
first step: starting from the root;
Step: Access the node;
third step: to determine whether the node is not accessible to a child node, if so, then it turned to the left-most sub-sections have not been accessed, and the second step, otherwise the fourth step;
the fourth step : If the node is the root node, the access is completed, otherwise the fifth step;
step five: return to the parent node of the node, and perform the third step.
In short: the process of scanning the entire tree structure that is, in order tree traversal process.
data stored in the tree structure table, the hierarchical relationships between data, i.e., parent-child relationship, to describe the relationships between columns and the columns in the table by, as the EMP table EMPNO and MGR, indicating that the employee EMPNO No, MGR who represent the leadership of the employee's number, MGR value that is equal to the EMPNO value of the child node of the parent node. In each row of the table has a parent node represents MGR (except the root node), each parent node by node, can determine the entire tree structure.
Use START WITH and CONNECT BY clauses can query the tree structure table in the SELECT command, the command format is as follows:
1 the SELECT ... 2 the CONNECT BY {PRIOR is the column name 1 = column name 2 | column name 1 = PRIOR is split name} 2 . 3 [ the START the WITH ] ;
START WITH: not only can specify a root node, you can specify multiple root nodes.
position PRIOR operator is placed before and after the equal sign, and determines the search sequence when the query.
PRIOR when the CONNECT BY clause is placed in front of the middle number, is forced from the root node to sequentially retrieved leaf node, i.e. the child nodes of the tree structure through the direction indicated by the parent node, which we call top-down manner. As:
. 1 the CONNECT BY PRIOR is the EMPNO = MGR
When PIROR operator is placed behind the CONNECT BY clause, moderate number, is forced to order the root node is retrieved from the leaf node, i.e. the child node to the parent node of the tree structure through direction, which we call a bottom-up manner. E.g:
also specify a start node in this manner.
in the query tree top-down structure, not only from the root node, any node can also be defined as the starting node, in order to start looking down. The results of this look is to the node of a tree begins.
table having a tree structure, each row of data is a node in the tree, since different nodes in which the level position, the rows can each have a number of layers. The layer number is determined from the root node. No matter from which node starts, the initial root level number is always 1, the child of the root node is 2, and so on. Figure 1.2 shows the hierarchical tree structure on the.
when the query tree structure, can remove some of the rows of the table, you can cut a tree branch, a WHERE clause to define a single node in the tree structure, the tree to remove a single node, but it does not affect its descendant nodes (when top-down retrieval) or predecessor node (top to bottom when retrieved).
as in other queries in the query tree structure may be used ORDER BY clause, change the order of search results, rather than in the order of traversal of the tree structure.
1 SQL> select * from test; 2 BILL_MONTH DAY_NUMBER MSISDN 3 -------------------- ---------- -------------------- 4 200803 1 13800 5 200803 3 13800 6 200803 2 13800 7 200803 2 13801 8 200803 4 13804 9 200803 5 13804 10 200803 7 13804 11 200803 8 13804 12 200803 6 13802 13 200803 6 13801 14 200803 7 13801 15 200803 8 13801 16 rows selected 17 18 SQL> 19 SQL> select * from test 20 2 start with day_number=1 21 3 connect by prior day_number=day_number-1 and prior msisdn= msisdn 22 4 ; 23 24 BILL_MONTH DAY_NUMBER MSISDN 25 -------------------- ---------- -------------------- 26 200803 1 13800 27 200803 2 13800 28 200803 3 13800
上面的语句查找出了从1开始,并且day_number 逐渐+1 递增的,并且 msisdn 相同的哪些个数据.
2. start with connect by 语法结构
如上面说看到的例子,其语法结构为start with condition connect by condition (含 prior 关键字)
start with conditon 给出的seed 数据的范围, connect by 后面给出了递归查询的条件,prior 关键字表示父数据,prior 条件表示子数据需要满足父数据的什么条件。在下面的这个start with connect by 结构中,就表示查找出了从1开始,父数据的day_number等于子数据的day_number-1而且父数据的msisdn=子数据的msisdn.
start with day_number=1
connect by prior day_number=day_number-1 and prior msisdn= msisdn