有個空間

          有個標題

          Oracle 迭代查詢

          connect by prior start with 經常會被用到一個表中存在遞歸關系的時候。比如我們經常會將一個比較復雜的目錄樹存儲到一個表中。或者將一些部門存儲到一個表中,而這些部門互相有隸屬關系。這個時候你就會用到connect by prior start with。

           表結構:

          create table TB_ORG
          (
            ORGID            VARCHAR2(50) not null,
            ORGNAME          VARCHAR2(100),
            ORGPARENT        VARCHAR2(50),
          )

          SQL> select orgid,ORGPARENT from tb_org order by ORGid desc;

          ORGID                                              ORGPARENT
          -----------------                                   --------------------------------------------------
          top_org                                            
          newsroom                                      top_org

          sjgt                                                 newsroom
          bgjsyj                                             newsroom
          bgjs                                                newsroom
          test52                                            bgjsyj
          test36                                            bgjsyj
          test3                                              bgjs
          test2                                              sjgt
          test1                                              sjgt
          tes4t1                                             bgjs

          --子取父
          select * from tb_org CONNECT BY PRIOR orgparent=orgid START WITH orgid='newsroom'
          -----子取父變形 ()
          select * from tb_org CONNECT BY PRIOR orgparent=orgid START WITH orgparent='newsroom'
          --父取子(結果中沒有父這條記錄)
          select * from tb_org CONNECT BY PRIOR orgid=orgparent START WITH orgparent='newsroom'
          -----父取子變形(多了父這一級)
          select * from tb_org connect by prior orgid=orgparent start with orgid ='newsroom'

           

           

           

          注意orgid=orgparent 和orgparent=orgid


          ref:http://blog.csdn.net/kenny1985/article/details/3980583

          posted on 2012-11-28 14:13 游雯 閱讀(6477) 評論(0)  編輯  收藏 所屬分類: Java技術

          主站蜘蛛池模板: 香格里拉县| 贵定县| 宁化县| 新安县| 龙泉市| 安宁市| 黑水县| 鄂伦春自治旗| 北票市| 益阳市| 轮台县| 运城市| 龙口市| 内江市| 哈巴河县| 常州市| 竹山县| 云阳县| 乌什县| 扶风县| 盐亭县| 商丘市| 土默特左旗| 分宜县| 山西省| 得荣县| 武鸣县| 崇阳县| 清苑县| 宜州市| 沧州市| 江津市| 修水县| 安多县| 杨浦区| 梧州市| 桑日县| 宁河县| 连云港市| 靖西县| 苍梧县|