ORACLE 트 리 조작

5957 단어 Oacle
더 읽 기
Oracle        Oracle          select...start with... connect by ...prior    。      ,                     。      Oracle                          Oracle     ,                            。 
                    ,     :

CREATE TABLE FLFL
(
  ID      NUMBER                                NOT NULL,
  MC      NVARCHAR2(20),
  FLJB    NUMBER,
  SJFLID  NUMBER
)
      FLJB       ,          SQL     。                   。

      SJFLID      ID,        , SJFLID null(     ,          ,      ,      null  ,        ,    0  )。

               ,             ,                   。

 

      1.             (      )。            ,                  ,             。

SELECT * FROM flfl WHERE sjflid IS NULL;
           ,       。

 

      2.            (    )。             ,           。

SELECT * FROM flfl WHERE sjflid = 819459;
            ID 819459       。

 

      3.               (    )。 

SELECT * FROM flfl START WITH ID = 819459 CONNECT BY sjflid = PRIOR ID;
            ID 819459             ,                。

 

      4.            (  )。               ,           。

SELECT b.* FROM flfl a JOIN flfl b ON a.sjflid = b.ID WHERE a.ID = 6758;
            ID 6758         ,           。

 

      5.              (  )。 

SELECT * FROM flfl START WITH ID = 6758 CONNECT BY PRIOR sjflid = ID;
             ID 6758        ,              、   。                                   ,         。

 

                  , 3     5   ,            prior        ,            。  sjflid = PRIOR ID ,         ID   sjflid  ID     ,                   ; PRIOR ID = sjflid ,         sjflid        sjflid   id   ,                  。

                         ,                ,             。

 

      6.           (   )。 

SELECT a.*
  FROM flfl a
 WHERE EXISTS (SELECT *
                 FROM flfl b
                WHERE a.sjflid = b.sjflid AND b.ID = 6757);
              ID 6757              ,       。

 

      7.            (   )。              ,    FLJB,             ,               ,               !

WITH tmp AS
     (SELECT     a.*, LEVEL lev
            FROM flfl a
      START WITH a.sjflid IS NULL
      CONNECT BY a.sjflid = PRIOR a.ID)
SELECT *
  FROM tmp
 WHERE lev = (SELECT lev
                FROM tmp
               WHERE ID = 819394)
               ,      LEVEL             ,      with                。 

 

      8.                (     )。 

WITH tmp AS
     (SELECT     flfl.*, LEVEL lev
            FROM flfl
      START WITH sjflid IS NULL
      CONNECT BY sjflid = PRIOR ID)
SELECT b.*
  FROM tmp b,
       (SELECT *
          FROM tmp
         WHERE ID = 7004 AND lev = 2) a
 WHERE b.lev = 1
UNION ALL
SELECT *
  FROM tmp
 WHERE sjflid = (SELECT DISTINCT x.ID
                            FROM tmp x,
                                 tmp y,
                                 (SELECT *
                                    FROM tmp
                                   WHERE ID = 7004 AND lev > 2) z
                           WHERE y.ID = z.sjflid AND x.ID = y.sjflid);
                 。  ,  7   ,             ;  ,            ,          ,     :(1)         ,      lev  1,         ,    。(2)     2   ,     lev  2,       lev   1             。(3)      3      ,                 (  ),                            。   ,    UNION              ,     。

 

      9.               (  )。 

            7       。

WITH tmp AS
     (SELECT     a.*, LEVEL lev
            FROM flfl a
      START WITH a.sjflid IS NULL
      CONNECT BY a.sjflid = PRIOR a.ID)
SELECT *
  FROM tmp
 WHERE lev = (SELECT lev
                FROM tmp
               WHERE ID = 819394) - 1
                  。

 

         ,         ,         。  ,              ,           ,              ,            ,           。

            ,       ,                    ,       ,     start with     。 

                       。

 

      10.           。 

                ,         ,         (      );          ,         (     )。     :          、  、  、     ,          (    ,         ,       )。

           :

SELECT     SYS_CONNECT_BY_PATH (mc, '/')
      FROM flfl
     WHERE ID = 6498
START WITH sjflid IS NULL
CONNECT BY sjflid = PRIOR ID;
             :

SELECT     SYS_CONNECT_BY_PATH (mc, '/')
      FROM flfl
START WITH ID = 6498
CONNECT BY PRIOR sjflid = ID;
                   。oracle      sys_connect_by_path  ,            。       ,   SQL         ,    SQL         ,             ,        SQL        ,    SQL          。  PS  。

      sys_connect_by_path     start with         ,          ,start with           ,                ,         ,          。 

 

      11.          。 

           ,     start with     。

SELECT     CONNECT_BY_ROOT mc, flfl.*
      FROM flfl
START WITH ID = 6498
CONNECT BY PRIOR sjflid = ID;
      connect_by_root        ,               。 

 

      12.           。 

            ,        ,               ,         。 

SELECT     CONNECT_BY_ISLEAF, flfl.*
      FROM flfl
START WITH sjflid IS NULL
CONNECT BY sjflid = PRIOR ID;
      connect_by_isleaf                  ,      ,        ,    0;  ,         ,    1。 

좋은 웹페이지 즐겨찾기