2
我買了兩個數據表,列出如下:逗號分隔結果集+ SQL查詢
表1:學生
表2:主題
我需要輸出爲:
我這來達到的,使用了XML路徑下查詢
代碼:
WITH cte
AS (SELECT Stu.Student_Id ,
Stu.Student_Name ,
(SELECT Sub.[Subject] + ','
FROM [Subject] AS Sub
WHERE Sub.Student_Id = Stu.Student_Id
ORDER BY Sub.[Subject]
FOR
XML PATH('')
) AS [Subjects]
FROM dbo.Student AS Stu
)
SELECT Student_id [Student Id] ,
student_name [Student Name] ,
SUBSTRING(Subjects, 1, (LEN(Subjects) - 1)) AS [Student Subjects]
FROM cte
我的問題是有沒有更好的方式來做到這一點,而不使用XML路徑?