Oracle recursive query (all subsets under query condition ID)

Source: Internet
Author: User

First, CREATE TABLE tbl_test
(
ID number,
NAME VARCHAR2 (+),
PID number DEFAULT 0
)
INSERT into Tbl_test (id,name,pid) VALUES (' 1 ', ' 10 ', ' 0 ');
INSERT into Tbl_test (id,name,pid) VALUES (' 2 ', ' 11 ', ' 1 ');
INSERT into Tbl_test (id,name,pid) VALUES (' 3 ', ' 20 ', ' 0 ');
INSERT into Tbl_test (id,name,pid) VALUES (' 4 ', ' 12 ', ' 1 ');
INSERT into Tbl_test (id,name,pid) VALUES (' 5 ', ' 121 ', ' 2 ');
Second, format
Select * from .... Where [result filter condition statement]
Start with [and start condition filter statement]
Connect by prior [and intermediate record filter condition statement]
Third, find all subordinates
SELECT * from tbl_test start with id=1 connect by prior Id=pid
Note: This SQL can look up all the subordinates of the id=1 data, note that when writing the SQL statement, the condition of the connect by prior clause is id=pid because it is looking for subordinates from the ID.
Iv. Find All Superiors
SELECT * from tbl_test start with id=5 connect by prior Pid=id
Because you are looking for a superior from the ID, the condition of the connect by prior clause is Pid=d

Oracle recursive query (all subsets under query condition 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.