千家信息网

SQL字符串合并

发表于:2024-09-21 作者:千家信息网编辑
千家信息网最后更新 2024年09月21日,create table #tb(id int, value varchar(10),cname varchar(20))insert into #tb values(1, 'aa','aaaa')i
千家信息网最后更新 2024年09月21日SQL字符串合并

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 #tbgroup by id---不去重select id, [value] = stuff((select ',' + [value] from #tb t where id = #tb.id for xml path('')) , 1 , 1 , ''),max(isnull(cname,''))from #tbgroup by id

drop table #tb


0