现有2张表,学生表Tb_Student和成绩表Tb_Result,如图:

 

         

  Tb_Student         Tb_Result

 

现在想要实现查询出如下的结果:

 

 

即每个学生的所有成绩放在一列里,这就是典型的行转列的问题。

 

使用sql中的for xml path即可实现,

select Stu_id,Stu_name,

    (select class + ':' + cast(score as varchar) + ','

    from Tb_Result where Stu_id=s.Stu_id

    for xml path('')) as result

from Tb_Student s

相关文章:

  • 2022-12-23
  • 2021-08-21
  • 2021-11-28
  • 2022-12-23
  • 2021-09-01
  • 2021-06-09
  • 2021-12-27
  • 2022-01-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-28
  • 2021-12-06
  • 2021-07-20
  • 2022-12-23
相关资源
相似解决方案