SQL分组后拼接字符串with t  as(
select'Charles' parent, 'William' child union
select'Charles', 'Harry'union
select'Anne', 'Peter'union
select'Anne', 'Zara'union
select'Andrew', 'Beatrice'union
select'Andrew', 'Eugenie'
)
SELECT parent,
STUFF(
(
SELECT','+ child FROM t a WHERE b.parent = a.parent FOR XML PATH('')
),1 ,1, '') children字符串截取 sql
FROM t b
GROUP BY parent
原表:
Parent Child
Charles William
Charles Harry
Anne Peter
Anne Zara
Andrew Beatrice
Andrew Eugenie
处理后的结果:
Parent Children
Charles William,Harry
Anne Peter,Zara
Eugenie,Beatrice
Andrew

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。