oracle和postgresql 遞迴查詢父子關聯性記錄文法區別

來源:互聯網
上載者:User

標籤:_id   acl   code   pid   條件   節點   查詢   ble   區別   

oracle:

一、資料

db資料欄位如下:

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

二、格式
        Select * from …. Where [結果過濾條件陳述式]
  Start with  [and起始條件過濾語句]
  Connect by prior [and中間記錄過濾條件陳述式]
三、尋找所有下級
        select * from tablename start with id=1 connect by prior id=pid
  注意:此sql能尋找id=1的資料的所有下級,寫sql語句時要注意,因為是從id開始尋找下級,所以connect by prior 子句的條件是         id=pid
四、尋找所有上級
       select * from tablename start with id=5 connect by prior pid=id
  因為是從id開始尋找上級,所以connect by prior 子句的條件是pid=d

select t.task_id ,t.task_name ,t.parent_task_id from t_task t start with task_id=‘000001‘connect by prior task_id = parent_task_id;

五、顯示結果

結果顯示:

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

postgresql:

查詢父節點下所有的子節點

WITH recursive fileinfo (pk_fi_id,  f_fi_parentid)AS(SELECT        pk_fi_id ,        f_fi_parentid     FROM        t_fileinfo    WHERE        pk_fi_id = ‘92719f78-22d6-4db1-a484-dff34de76890‘UNION ALLSELECT            mm.pk_fi_id ,            mm.f_fi_parentid         FROM            t_fileinfo AS mmINNER JOIN fileinfo AS child ON mm.f_fi_parentid = child.pk_fi_id)SELECT  *FROM fileinfo

 

oracle和postgresql 遞迴查詢父子關聯性記錄文法區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.