Luffy: "Transfer the original CSDN blog to the blog park!" ”
Some time ago, the task that I was responsible for was involved in the business requirement of organization relationship, and I used Oracle recursive query. Here's a quick example. At work, we often encounter a relationship with a certain level of organization. For example, how many cities are saved, and there are multiple districts under each city. Or the affiliation of the company organization department. At this point we may use the start with the Connect by prior recursive query
Examples of usage
1. Basic SQL Syntax :
select ... from + 表 start with + 条件1connect by prior + 条件2 where + 条件3 条件说明: 条件1:是根节点的限定语句 条件2:连接条件。prior表示上一条信息。比如connect by prior org_id = parent_id 就是说上一条记录的org_id是本条 记录的parent_id,即本记录的父亲是上一条记录。 条件3:过滤条件
2. Examples of usage:
Table name: City_tree, table structure is as follows:
Table data:
=======================================================================
(1) query down from the root node:
select A.*from CITY_TREE ASTARTWITH A.CITY_ID =‘1‘CONNECTBYPRIOR A.CITY_ID = A.CITY_PID;
Results such as:
(2) Look up from the root section 1 points
select A.* from CITY_TREE A START WITH A.CITY_ID = ‘1‘CONNECT BY A.CITY_ID = PRIOR A.CITY_PID;
Results such as:
Recursive query of Oracle Advanced function article start with connect by prior simple usage