这是创建表和一些数据的脚本。
--Students table ---------
CREATE TABLE [Students](
[ID] [int] NOT NULL,
[SubjectID] [int] NULL,
[StudentName] [nvarchar](50) NULL,
[ConcatTo] [bit] NULL
) ON [PRIMARY]
GO
INSERT [Students] ([ID], [SubjectID], [StudentName], [ConcatTo]) VALUES (1, 1, N'Mary', 1)
GO
INSERT [Students] ([ID], [SubjectID], [StudentName], [ConcatTo]) VALUES (2, 1, N'Brown', NULL)
GO
INSERT [Students] ([ID], [SubjectID], [StudentName], [ConcatTo]) VALUES (3, 2, N'Lily2', NULL)
GO
INSERT [Students] ([ID], [SubjectID], [StudentName], [ConcatTo]) VALUES (4, 2, N'Michilin2', 1)
GO
INSERT [Students] ([ID], [SubjectID], [StudentName], [ConcatTo]) VALUES (5, 2, N'Joshua2', NULL)
GO
select *from Students;
SELECT Main.SubjectID, main.Students As "Students"
FROM
(
SELECT DISTINCT t2.SubjectID,
(
SELECT t1.StudentName + ' ' AS [text()]
FROM dbo.Students t1
WHERE t1.SubjectID = t2.SubjectID
FOR XML PATH ('')
) Students
FROM dbo.Students t2
) Main
从表中选择会有这个
最多我只知道这样选择,但我不知道如何像这样更新我的学生表
这是我对预期结果的截图。
如何使用“Lily2 Michilin2 Joshua2”更新 StudentName 列,其中 ConcatTo = 1 就像我的选择语句一样?然后去掉 Lily2 和 Joshua2 行?