Create string from array

前端 未结 3 1459
离开以前
离开以前 2021-01-14 23:56

I have a table in PostgreSQL that contains:

id   name   arrayofparents
1     First
2     Second      {1}
3     Second_Sec  {1,2}
4     Third       {1,2,3}
5          


        
3条回答
  •  再見小時候
    2021-01-15 00:51

    If you only want direct parents (not grandparents) then something like this should work:

    SELECT c.id, c.name, string_agg(p.name, '->') AS parentnames
    FROM yourtable AS c
      LEFT JOIN yourtable AS p ON p.id = ANY c.parents
    GROUP BY c.id, c.name
    

提交回复
热议问题