Oracle 查詢父級以及以下的數據 & 刪除父級以及以下的數據

1.  語法格式
        Select * from …. Where [結果過濾條件語句]
                       start with  [and起始條件過濾語句]
                       connect by prior [and中間記錄過濾條件語句]
2.  查找所有下級
        例如: select * from tbl_test start with id=1 connect by prior id=pid

        如此: select t.*, t.rowid  from CM_BUSINESS_CODE t  

                        start with t.business_code ='111'  
                        connect by prior t.business_code = t.super_business_code
注意:此sql能查找id=1的數據的所有下級,寫sql語句時要注意,因爲是從id開始查找下級,所以connect by prior 子句的條件是         id=pid
3.  查找所有上級
       select * from tbl_test start with id=5 connect by prior pid=id
因爲是從id開始查找上級,所以connect by prior 子句的條件是pid=d

 

4. 刪除父級以及以下的數據

delete from
CM_BUSINESS_CODE
where business_code
in
(  select business_code  from CM_BUSINESS_CODE t  start with t.business_code =#{businessCode}
connect by prior t.business_code = t.super_business_code)

 

 

發佈了59 篇原創文章 · 獲贊 18 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章