日期:2014-05-17  浏览次数:20610 次

如何通过表a得到表b


就是把每个人对应的课程用逗号连起来,求指教谢谢

------解决方案--------------------



if object_id('Tempdb..#t') is not null drop table #t
create table #t(
id int identity(1,1) not null,
lastname nvarchar(100) null,
lession nvarchar(100) null
)
Insert Into #t
select '张三','数学' union all
select '张三','语文' union all
select '李四','数学' union all
select '张三','英语' union all
select '王五','语文' union all
select '李四','语文' union all
select '张三','物理'

select t.lastname,stuff((select ','+lession from #t z where z.lastname=t.lastname for xml path('')),1,1, '')
from #t t group by lastname
 
-----------------

(7 行受影响)
lastname                                                                                             
---------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------
李四                                                                                                   数学,语文