Oracle and PostgreSQL recursive query parent-child relationship record syntax differences

Source: Internet
Author: User

Oracle

First, the data

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                          ******                      ***                         ***                                   ***

Second, format
        Select * from .... Where [result filter condition statement]
start with  [and start conditional filter statement]
Connect by prior [and intermediate record filter statement]
Three, find all subordinates
    &N Bsp   SELECT * FROM tablename start with id=1 connect by prior id=pid
Note: This SQL can find all the subordinates of the id=1 data, note that when writing the SQL statement, it is necessary to look at the subordinate from the ID start , so the conditions of the connect by prior clause are         Id=pid
Four, find all superiors
       select * from Tabl ename 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

  Select from with task_id = ' 000001 '  by= parent_task_id;

V. Display of results

The results show:

task_id                  Task_name          t.parent_task_id000001                 t1           000002                 t11                        000001000003                 t111                      000002000004                 t1111                     000003000005                 t12                         000001000006                 t121                      000005000007                 t1211                    000006

PostgreSQL

Querying all child nodes under the parent node

 withRecursive FileInfo (pk_fi_id, F_fi_parentid) as(SELECTpk_fi_id, F_fi_parentid fromT_fileinfoWHEREpk_fi_id= '92719f78-22d6-4db1-a484-dff34de76890'UNION  AllSELECTmm.pk_fi_id, Mm.f_fi_parentid fromT_fileinfo asmmINNER JOINFileInfo asChild onMm.f_fi_parentid=child.pk_fi_id)SELECT  * fromFileInfo

Oracle and PostgreSQL recursive query parent-child relationship record syntax differences

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.