简体   繁体   English

将所有行列从连接表合并为单列

[英]merge all rows columns into single column from joined table

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).我正在考虑与 parent_id 分组,但获得多条记录(每个孩子一条记录)。 What logic i can implement here apart from grouping to get all child's for a parent in single row.除了分组以在单行中为父级获取所有子级之外,我可以在这里实现什么逻辑。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM