id value
1 1 aa
2 1 bb
3 2 aaa
4 2 bbb
5 2 ccc
求结果:
id value
1 1 aa,bb
2 2 aaa,bbb,ccc
代码如下:
if OBJECT_ID('aaa_t')is not null
drop table aaa_t;
create table aaa_t
(id int,
value varchar(20));
INSERT aaa_t SELECT 1, 'aa'
UNION ALL SELECT 1, 'bb'
UNION ALL SELECT 2, 'aaa'
UNION ALL SELECT 2, 'bbb'
UNION ALL SELECT 2, 'ccc';
查询结果:
select a.id,stuff((select DISTINCT ','+CAST(value AS VARCHAR) from aaa_t as b
where b.id =a.id
for xml path('')),1,1,'') 'value'
from aaa_t a
group by a.id
请问这句话什么意思:select DISTINCT ','+CAST(value AS VARCHAR) from aaa_t as b
where b.id =a.id
for xml path('') ------解决方案-------------------- 参考一下吧:http://www.cnblogs.com/doubleliang/archive/2011/07/06/2098775.html ------解决方案-------------------- select DISTINCT ','+CAST(value AS VARCHAR) from aaa_t as b
where b.id =a.id
for xml path('')