Oracle recursive query

Source: Internet
Author: User

Grammar:

Select *  from  with column = ? Connect by [[Prior] T.parentid

For example: Menu table, table structure is as follows

  

The data are as follows:

  

First, from the top down

1. Query all sub-menus for a specified ID

Select *  from  with=1 by= T. " PARENT "

Note: From the top down, is to specify a node, query its under all the child nodes (including direct child nodes and non-direct child nodes), so start with the condition of the parent =?;

The connect by is the relationship between record and record when the recursion is specified;

The most important distinction is prior: from the top down, 1 as the parent, so the prior is placed on the other side of the parent field, which means that all the child entries are queried

2. Query all subordinate menus of a given ID and menu information for that specified ID

Select *  from  with=1by = t. "  PARENT "

Note: From the top down query, find all child nodes under a specified node and the specified node, so the start with condition Specifies ID =? , which means id =? Data starts to query down (contains the specified ID entry).

The connect by is the relationship between record and record when the recursion is specified;

The most important distinction is prior: from the top down, 1 as the parent, so the prior is placed on the other side of the parent field, which means that all the child entries are queried

Second, from the bottom up query

1. Query all parent menus for a given ID

Select *  from  with=prior T. "   PARENT "

Note: Query from bottom up, only "contains this record" query, which is different from the top down query

Prior location: Take 42 as ID, get all of its parent, so put prior on the parent side

Iii. Summary

1, from the top down query, you can have two results: contains the specified entry, does not contain the specified entry

2, from the bottom up query, there is only one result: contains the specified entry

3. Location of Prior:

Query from top to bottom: start with column =?

? As a parent, prior on the child side, representing all the children of the query

Query from bottom up: start with column =?

? As a child, prior on one side of the parent, indicating that all parents are queried

Oracle recursive query

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.