oracle中使用SQL遞迴語句

來源:互聯網
上載者:User

例子:

 pid  id
  a   b  
  a   c    
  a   e  
  b   b1  
  b   b2  
  c   c1  
  e   e1  
  e   e3  
  d   d1  
   
  指定pid=a,選出  
  a   b  
  a   c    
  a   e  
  b   b1  
  b   b2  
  c   c1  
  e   e1  
  e   e3 
SQL語句:select   parent,child   from   test   start   with   pid='a'   
connect   by   prior   id=pid 

 

 

Oracle  SQL的遞迴查詢:
1、表機構
SQL> desc comm_org_subjection
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ORG_SUBJECTION_ID                         NOT NULL VARCHAR2(32)    子鍵
 ORG_ID                                    NOT NULL VARCHAR2(32)
 FATHER_ORG_ID                             NOT NULL VARCHAR2(32)         父鍵
 LOCKED_IF                                 NOT NULL VARCHAR2(1)
 START_DATE                                NOT NULL DATE
 END_DATE                                           DATE
 EDITION_NAMEPLATE                                  NUMBER(8)
 CODE_AFFORD_IF                                     VARCHAR2(1)
 CODE_AFFORD_ORG_ID                        NOT NULL VARCHAR2(32)
 CODING_SHOW_ID                                     NUMBER(8)
 BSFLAG                                             VARCHAR2(1)
 MODIFI_DATE                                        DATE
 CREATOR_ID                                         VARCHAR2(32)
 CREATE_DATE                                        DATE
 CREATOR                                            VARCHAR2(35)

2、遞迴尋找父結點 org_id為C6000000000001下的所有子結點:
select * from comm_org_subjection a
start with a.org_id='C6000000000001'
connect by prior a.org_subjection_id=a.father_org_id

3、遞迴尋找子結點 org_id為C6000000000001下的所有父結點:
select org_id from comm_org_subjection a
start with a.org_id='C6000000000001'
connect by prior a.father_org_id=a.org_subjection_id

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.