34,590
社区成员
发帖
与我相关
我的任务
分享
--这样?
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (a int,subject nvarchar(4))
insert into [TB]
select 1,'语文' union all
select 1,'数学' union all
select 1,'英语' union all
select 2,'语文' union all
select 2,'物理'
select * from [TB]
select a, [subject]=stuff((select ','+[subject] from tb t where a=tb.a for xml path('')), 1, 1, '')
from tb
WHERE a=1
group by a
/*
a subject
1 语文,数学,英语
*/