Use oracle recursive query to process parent-child relationship records

Source: Internet
Author: User

Oracle recursive query can process parent-child relationship records. The following describes how to use oracle recursive query to process parent-child relationship records for your reference.

The database often processes records of parent-child relationships. In oracle, you can use oracle recursive query statements to retrieve all the sub-records at a time. For example:
T1
T11
T111
T1111
T12
T121
T1211

The db data fields are as follows:
Task_id task_name t. parent_task_id ***
************
000001 t1 ******
000002 t11 000001 ***
000005 t12 000001 ***
000003 t111 000002 ***
000004 t1111 000003 ***
000006 t121 000005 ***
000007 t1211 000006 ***
************

Query statement:
Select t. task_id, t. task_name, t. parent_task_id
From t_task t s
Tart with task_id = '000000'
Connect by prior task_id = parent_task_id;

Result:
Task_id task_name t. parent_task_id
000001 t1
000002 t11 000001
000003 t111 000002
000004 t1111 000003
000005 t12 000001
000006 t121 000005
000007 t1211 000006

Strat with specifies the conditions for starting a hierarchy, that is, the rows that meet this condition can be used as the top layer of the hierarchy tree.

Connect by prior refers to the association condition between layers, that is, what kind of row is the upper-layer row's sub-row self-join condition)

Select level, id, name, parentid from temptable2
Connect by prior parentid (top-level column) = id (child-layer column) start with id = 1

Common ORACLE Data Types

How to query Oracle log files

Oracle index Optimization Design

Implementation of parallel query of one column in oracle

Use of oracle rownum statements

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.