An amazing use of Oracle hierarchical Query

Source: Internet
Author: User

Query all nodes and indent them by node.

Select * from organ connect by parent_id = prior ID start with parent_id is null;

The working method is to first find the record whose parent_id is null, and then expand it layer by layer according to the rules specified by parent_id = prior ID.

Here, prior indicates that the ID of the previous layer is equal to the parent_id extension of the current layer, which is equivalent to connect by this_row.parent_id = prior_row.id.

You can also use combination conditions in the connect by clause. In addition, if the conditions in connect by do not have prior, it is considered a simple pruning condition.

This usage is not new. today, we found a wonderful use. We can find all its parent Nodes Based on a node. This can be used to determine whether the child node belongs to the parent node. In this case, we can still use this hierarchical query. The query method is as follows:

Select * from organ connect by prior parent_id = ID start with ID = 861;

In this way, all upper-level nodes can be traced layer by layer.

Related Article

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.