sql >> Database teknologi >  >> RDS >> Oracle

SQL SELECT for at finde cykliske referencer i far-ID-organiseret træ?

SELECT  n.*, CONNECT_BY_ROOT(id), level
FROM    elements n
START WITH
        id IN
        (
        SELECT  MIN(id)
        FROM    (
                SELECT  id, CONNECT_BY_ROOT(id) AS root
                FROM    elements
                START WITH
                        id IN
                        (
                        SELECT  id
                        FROM    elements n
                        WHERE   CONNECT_BY_ISCYCLE = 1
                        CONNECT BY NOCYCLE
                                father_id = PRIOR id
                        )
                CONNECT BY NOCYCLE
                        id = PRIOR father_id
                )
        GROUP BY
                root
        )
CONNECT BY NOCYCLE
        id = PRIOR father_id

Du kan eventuelt læse denne artikel:




  1. Vælg forespørgsel, men vis resultatet fra post nummer 3

  2. Fejlrelation eksisterer ikke

  3. Jeg kan ikke få INSERT til at virke

  4. Oracle-ækvivalent til java System.currentTimeMillis()?