悟心

          成功不是將來才有的,而是從決定去做的那一刻起,持續(xù)累積而成。 上人生的旅途罷。前途很遠(yuǎn),也很暗。然而不要怕。不怕的人的面前才有路。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            93 隨筆 :: 1 文章 :: 103 評(píng)論 :: 0 Trackbacks
          Start withConnect By子句遞歸查詢一般用于一個(gè)表維護(hù)樹形結(jié)構(gòu)的應(yīng)用。
          創(chuàng)建示例表:
          CREATE TABLE TBL_TEST
          (
            ID    
          NUMBER,
            NAME  
          VARCHAR2(100 BYTE),
            PID   
          NUMBER                                  DEFAULT 0
          );
           
          插入測(cè)試數(shù)據(jù):
          INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('1','10','0');
          INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('2','11','1');
          INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('3','20','0');
          INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('4','12','1');
          INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('5','121','2');
           
          從Root往樹末梢遞歸
          select * from TBL_TEST
           start 
          with id=1
           connect 
          by prior id = pid
           
          從末梢往樹ROOT遞歸
          select * from TBL_TEST
           start 
          with id=5
           connect 
          by prior pid = id
          =====
          對(duì)于oracle進(jìn)行簡(jiǎn)單樹查詢(遞歸查詢)

          DEPTID PAREDEPTID NAME 
          NUMBER NUMBER CHAR (40 Byte) 
          部門id 父部門id(所屬部門id) 部門名稱 


          通過子節(jié)點(diǎn)向根節(jié)點(diǎn)追朔.

          Sql代碼 
          1.select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid   
          Sql代碼  
          1.select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid   
           
          select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid  

          通過根節(jié)點(diǎn)遍歷子節(jié)點(diǎn). 

          Sql代碼 
          1.select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid   
          Sql代碼  
          1.select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid   
          select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid  

          可通過level 關(guān)鍵字查詢所在層次. 

          Sql代碼 
          1.select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid   
          Sql代碼  
          1.select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid   
          select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid  

          再次復(fù)習(xí)一下:start 
          with connect by 的用法, start with 后面所跟的就是就是遞歸的種子。 

          遞歸的種子也就是遞歸開始的地方 connect 
          by 后面的"prior" 如果缺省:則只能查詢到符合條件的起始行,并不進(jìn)行遞歸查詢; 

          connect 
          by prior 后面所放的字段是有關(guān)系的,它指明了查詢的方向。 

          練習(xí): 通過子節(jié)點(diǎn)獲得頂節(jié)點(diǎn) 

          Sql代碼 
          1.select FIRST_VALUE(deptid) OVER (ORDER BY LEVEL DESC ROWS UNBOUNDED PRECEDING) AS firstdeptid from persons.dept start with deptid=76 connect by prior paredeptid=deptid  
          ====這種方法只是當(dāng)表里就有一顆樹,多棵樹怎么辦?

          .聲明:JavaEye文章版權(quán)屬于作者,受法律保護(hù)。沒有作者書面許可不得轉(zhuǎn)載。 
          posted on 2011-03-16 11:30 艾波 閱讀(699) 評(píng)論(0)  編輯  收藏 所屬分類: SQL
          主站蜘蛛池模板: 开远市| 盐池县| 湘潭市| 奉节县| 福贡县| 泸溪县| 安泽县| 雷山县| 鸡泽县| 鄂托克前旗| 郑州市| 凉山| 酒泉市| 丰台区| 吉林市| 江西省| 奉化市| 常宁市| 肥城市| 潞西市| 神池县| 中宁县| 云龙县| 虞城县| 彭泽县| 香河县| 错那县| 孟津县| 兴城市| 兴仁县| 安溪县| 嘉义市| 建始县| 秦安县| 昆山市| 兴仁县| 淳化县| 辉南县| 福鼎市| 雷州市| 清镇市|