merge all rows columns into single column from joined table

穿精又带淫゛_ 提交于 2021-02-05 11:33:26

问题


I want to join a table which has multiple rows and need to merge one of the column from all the rows into single column.

select a.parent_id,a.parent_name,concat(b.child_name) from parent a 
join children b on (a.parent_id=b.parent_id);

This should return all the parent rows and each parent row should have all its children's.

i am thinking to group with parent_id but getting multiple records (one record per child). What logic i can implement here apart from grouping to get all child's for a parent in single row.


回答1:


SELECT a.parent_id, a.parent_name, STRING_AGG(b.child_name, ',') as Children
FROM
    Parent a
    INNER JOIN children b
    ON a.Id = b.ParentId
GROUP BY
    a.parent_id
    ,a.parent_name


来源:https://stackoverflow.com/questions/39175634/merge-all-rows-columns-into-single-column-from-joined-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!