sql多行合并一列

with a as(
select * from(

select 1 userId  , '天津' province
union
select 1 userId  , '北京'
union
select 1 userId  , '上海'
union
select 2 userId  , '北京'
union
select 2 userId  , '上海'
) model
)

select distinct userId, stuff(
             (select ','+province  
             from a where userId=a2.userId for xml path('')
             ),1,1,'') 合并
 from a as a2

 

posted @ 2021-11-12 16:04  212的s  阅读(163)  评论(0编辑  收藏  举报