--利用stuff函数,for xml path('') 实现列的合并
CREATE TABLE dbo.[Student](
[id] [int] IDENTITY(1,1) NOT NULL,
[StuName] [varchar](50) NULL,
[StuSex] [varchar](10) NULL,
[StuAge] [int] NULL
)
go
--
select Student.StuName,
stuff((select ','+StuSex from Student as t where t.StuName= Student.StuName for xml path('')), 1, 1, '')
as Courses
from Student
group by Student.StuName
结果:
StuName Courses
-------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
gs 语文,数学
张三 男,未知
(2 行受影响)