如何通过表a得到表b

小V 2013-09-09 10:14:09


就是把每个人对应的课程用逗号连起来,求指教谢谢
...全文
168 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
發糞塗牆 2013-09-09
  • 打赏
  • 举报
回复
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 a.lastname,
stuff((select ','+lession from #t b 
       where b.lastname=a.lastname 
       for xml path('')),1,1,'') 'lession'
from #t a
group by  a.lastname

/*
lastname                                                                                             lession
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
李四                                                                                                   数学,语文
王五                                                                                                   语文
张三                                                                                                   数学,语文,英语,物理
*/
IEEE_China 2013-09-09
  • 打赏
  • 举报
回复



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                                                                                             
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
李四                                                                                                   数学,语文
王五                                                                                                   语文
张三                                                                                                   数学,语文,英语,物理

(3 行受影响)

andy_liucj 2013-09-09
  • 打赏
  • 举报
回复
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 * from #t

select distinct lastname, stuff((select ','+lession from #t b where a.lastname=b.lastname for XML path('')),1,1,'') lession from #t a

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧