34,873
社区成员
发帖
与我相关
我的任务
分享if object_id('[m]') is not null drop table [m]
go
create table [m]([jid] int,[no] int,[fid] int)
insert [m]
select 1,1003,5 union all
select 2,1003,6 union all
select 3,1005,65 union all
select 4,1006,81 union all
select 5,1006,89
go
create function f_str(@no varchar(20))
returns varchar(50)
as
begin
declare @s varchar(800)
select @s=isnull(@s+',','')+ltrim(fid)
from m
where no=@no
return @s
end
go
select no,dbo.f_str(no) as fid from m group by no
--测试结果:
/*
no fid
----------- --------------------------------------------------
1003 5,6
1005 65
1006 81,89
(所影响的行数为 3 行)
*/select
no,
stuff((select ','+ltrim(fid) from m where no=t.no for xml path('')),1,1,'')
from
m t
group by no