Recursive query of Oracle Advanced function article start with connect by prior simple usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.