当前在线人工客服
天达云-技术
QQ:1324027256
天达云-技术
QQ:1902643386
天达云-售前
QQ:1650874901
天达云-售前
QQ:2207995833
天达云-合作
QQ:1984186903
天达云微信小程序
400-837-6568

SQL字符串合并
更新:HHH   时间:2023-1-7


create table #tb(id int, value varchar(10),cname varchar(20))

insert into #tb values(1, 'aa','aaaa')

insert into #tb values(1, 'bb','eeee')

insert into #tb values(1, 'aa','tttt')

insert into #tb values(2, 'aaa','gggg')

insert into #tb values(2, 'bbb',null)

insert into #tb values(2, 'ccc','hhhh')

insert into #tb values(1, 'cc','llll')

go


----

---去重
select id, [value] = stuff((select distinct ',' + [value] from #tb t where id = #tb.id for xml path('')) , 1 , 1 , ''),max(isnull(cname,''))
from #tb
group by id
---不去重
select id, [value] = stuff((select ',' + [value] from #tb t where id = #tb.id for xml path('')) , 1 , 1 , ''),max(isnull(cname,''))
from #tb
group by id

drop table #tb


返回数据库教程...