首页 > 代码库 > SQL字符串合并
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 #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字符串合并
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。