两列数据:
id value
1 王
2 赵
3 李
4 张
1 姚
2 庄
得出效果:
1 王,姚
2 赵,庄
3 李
4 张
SQL:
select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
from tb
group by id
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22996654/viewspace-2738892/ ,如需转载,请注明出处,否则将追究法律责任。