当前位置:Gxlcms > 数据库问题 > SQL字符串合并

SQL字符串合并

时间:2021-07-01 10:21:17 帮助过:35人阅读

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


本文出自 “bamboo” 博客,请务必保留此出处http://hdf007.blog.51cto.com/42396/1920683

SQL字符串合并

标签:sql字符串合并

人气教程排行