询问者
sql中case when语句匹配不了多列

问题
-
表及数据如下
CREATE TABLE [dbo].[tZ_ProfessorEvent]( [UserId] [int] NULL, [ZJBH] [varchar](20) NULL, [XM] [varchar](200) NULL, [QuestCount] [int] NULL, [QuestTimes] [int] NULL, [VerifyCount] [int] NULL, [F20012026] [int] NULL, [T20012026] [int] NULL, [F20012027] [int] NULL, [T20012027] [int] NULL, [F20012028] [int] NULL, [T20012028] [int] NULL, [F20012029] [int] NULL, [T20012029] [int] NULL, [F20012031] [int] NULL, [T20012031] [int] NULL ) ON [PRIMARY] --文本初始数据 20012,20012,李四,,,,0,0,0,0,0,0,0,0,0,0 20013,20013,张丽,,,,0,0,0,0,0,0,0,0,0,0 20014,20014,张三,,,,0,0,0,0,0,0,0,0,0,0 CREATE TABLE [dbo].[tZ_t1]( [userId] [int] NULL, [taskFlowID] [int] NULL, [QuestCount] [int] NULL, [QuestTimes] [int] NULL ) ON [PRIMARY] --文本初始数据 20012,20012026,18,0 20012,20012027,8,0 20012,20012028,14,0 20013,20012028,14,0 20014,20012028,14,0 UPDATE [dbo].[tZ_ProfessorEvent] SET F20012026 = CASE tZ_t1.taskFlowID WHEN 20012026 THEN dbo.tZ_t1.QuestCount ELSE 0 end, F20012027 = CASE tZ_t1.taskFlowID WHEN 20012027 THEN dbo.tZ_t1.QuestCount ELSE 0 end, F20012028 = CASE tZ_t1.taskFlowID WHEN 20012028 THEN dbo.tZ_t1.QuestCount ELSE 0 end, F20012029 = CASE tZ_t1.taskFlowID WHEN 20012029 THEN dbo.tZ_t1.QuestCount ELSE 0 end FROM dbo.tZ_t1 WHERE dbo.tZ_t1.userId = [tZ_ProfessorEvent].UserId
如上所示,在进行update时,得到的结果只是匹配了 tZ_ProfessorEvent
第一行 20012 的F20012026,
第二行 20013 的F20012028
第三行 20014的F20012028
其它的列没有填充值。搞不明白为什么。求高手指教。
全部回复
-
UPDATE [dbo].[tZ_ProfessorEvent] SET
F20012026 = CASE tZ_t1.taskFlowID WHEN 20012026 THEN dbo.tZ_t1.QuestCount ELSE 0 end
FROM
dbo.tZ_t1 WHERE dbo.tZ_t1.userId = [tZ_ProfessorEvent].UserId
;UPDATE [dbo].[tZ_ProfessorEvent] SET
F20012027 = CASE tZ_t1.taskFlowID WHEN 20012027 THEN dbo.tZ_t1.QuestCount ELSE 0 end
FROM
dbo.tZ_t1 WHERE dbo.tZ_t1.userId = [tZ_ProfessorEvent].UserId
;UPDATE [dbo].[tZ_ProfessorEvent] SET
F20012028 = CASE tZ_t1.taskFlowID WHEN 20012028 THEN dbo.tZ_t1.QuestCount ELSE 0 end
FROM
dbo.tZ_t1 WHERE dbo.tZ_t1.userId = [tZ_ProfessorEvent].UserId
;UPDATE [dbo].[tZ_ProfessorEvent] SET
F20012029 = CASE tZ_t1.taskFlowID WHEN 20012029 THEN dbo.tZ_t1.QuestCount ELSE 0 end
FROM
dbo.tZ_t1 WHERE dbo.tZ_t1.userId = [tZ_ProfessorEvent].UserId
-
UPDATE [dbo].[tZ_ProfessorEvent] SET F20012026 = (SELECT B.QuestCount FROM dbo.tZ_t1 B WHERE B.userId = [tZ_ProfessorEvent].UserId AND B.taskFlowID = 20012026), F20012027 = (SELECT B.QuestCount FROM dbo.tZ_t1 B WHERE B.userId = [tZ_ProfessorEvent].UserId AND B.taskFlowID = 20012027), F20012028 = (SELECT B.QuestCount FROM dbo.tZ_t1 B WHERE B.userId = [tZ_ProfessorEvent].UserId AND B.taskFlowID = 20012028), F20012029 = (SELECT B.QuestCount FROM dbo.tZ_t1 B WHERE B.userId = [tZ_ProfessorEvent].UserId AND B.taskFlowID = 20012029);