Recursive query for hirarchical data based on adjacency list

后端 未结 2 361
我在风中等你
我在风中等你 2021-01-16 09:58

Learing SQL, and have a bit of a problem. I have 2 tables level and level_hierarchy

|name        | id |     |parent_id | child_id|
         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-16 10:32

    Your query for the first level (here depth to distinguish from the table) should look like this:

    select l.name, h.child_id, 1 as depth 
    from level l
    join level_hierarchy h on l.id = h.child_id 
    where h.parent_id is null;
    
       name   | child_id | depth 
    ----------+----------+-------
     Level1_a |        1 |     1
    (1 row)
    

    Note the proper use of is null (do not use = to compare with null as it always gives null).

    You can use the above as an initial query in a recursive cte:

    with recursive recursive_query as (
        select l.name, h.child_id, 1 as depth 
        from level l
        join level_hierarchy h on l.id = h.child_id 
        where h.parent_id is null
    union all
        select l.name, h.child_id, depth + 1
        from level l
        join level_hierarchy h on l.id = h.child_id
        join recursive_query r on h.parent_id = r.child_id
    )
    select *
    from recursive_query
    -- where depth = 2
    
       name   | child_id | depth 
    ----------+----------+-------
     Level1_a |        1 |     1
     Level2_b |        3 |     2
     Level2_a |       19 |     2
     Level3_a |        4 |     3
     Level3_b |        5 |     3
     Level4_a |        6 |     4
     Level4_b |        7 |     4
    (7 rows)    
    

提交回复
热议问题