Oracle start with connect by prior usage

Source: Internet
Author: User

Syntax:
Select *
From table name
Where condition 1
Start with condition 2
Connect by prior current table field = cascade table Field
The start with and connect by prior statements complete recursive records to form a tree structure, which can be used in tables with hierarchies.
Start with indicates the start record.
Connect by prior specifies the field relationship associated with the current record

Code:
-- Create a department table, which has a hierarchical structure. The child record is associated with the parent Record ID through parent_id.
Create Table dept (
ID number (9) primary key, -- department ID
Name varchar2 (100), -- department name
Parent_id number (9) -- parent department ID, which is used to associate with the parent department
);
Insert the following data into the table. to simplify the code, a department has only one subordinate department.
● Query recursive records from the root node
Select *
From Dept
Start with ID = 1
Connect by prior id = parent_id;

The following is the query result. Start with ID = 1 indicates that the query starts from the record with ID = 1, recursively returning to the leaf. The recursion condition is id = parent_id, the ID of the current record is equal to the parent_id of the subrecord.

● Query recursive records from the leaf node
Select *
From Dept
Start with ID = 5
Connect by prior parent_id = ID;

The following is the query result. The recursive condition is based on the parent_id of the current record and the ID of the parent record.
● Filter query results
Select *
From Dept
Where name like '% sales %'
Start with ID = 1
Connect by prior id = parent_id;

In the following query results, we can see that the start with... connect by prior is used to query the tree structure, and then the where condition takes effect. All query results are filtered.
● Role of prior
The prior keyword indicates that no recursive query is performed. Only records with ID = 1 are queried. The following is the result after removing the prior keyword from the first query.
Select *
From Dept
Start with ID = 1
Connect by prior id = parent_id;

 

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.