Find Parent Recursively using Query

后端 未结 2 1621
南笙
南笙 2020-12-02 07:40

I am using postgresql. I have the table as like below

parent_id    child_id
----------------------
101       102
103       104
104       105
105       106            


        
2条回答
  •  孤城傲影
    2020-12-02 08:22

    Use WITH RECURSIVE to create a Common Table Expression (CTE). For the non-recursive term, get the rows in which the child is immediately below the parent:

    SELECT
       c.child_id,
       c.parent_id
    FROM
       mytable c
    LEFT JOIN
       mytable p ON c.parent_id = p.child_id
    WHERE
       p.child_id IS NULL
    
     child_id | parent_id
    ----------+-----------
          102 |       101
          104 |       103
    

    For the recursive term, you want the children of these children.

    WITH RECURSIVE tree(child, root) AS (
       SELECT
          c.child_id,
          c.parent_id
       FROM
          mytable c
       LEFT JOIN
          mytable p ON c.parent_id = p.child_id
       WHERE
          p.child_id IS NULL
       UNION
       SELECT
          child_id,
          root
       FROM
          tree
       INNER JOIN
          mytable on tree.child = mytable.parent_id
    )
    SELECT * FROM tree;
    
     child | root
    -------+------
       102 |  101
       104 |  103
       105 |  103
       106 |  103
    

    You can filter the children when querying the CTE:

    WITH RECURSIVE tree(child, root) AS (...) SELECT root FROM tree WHERE child = 106;
    
     root
    ------
      103
    

提交回复
热议问题