none
sql中case when语句匹配不了多列 RRS feed

  • 问题

  • 表及数据如下

    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

    其它的列没有填充值。搞不明白为什么。求高手指教。

    2014年3月24日 5:45

全部回复

  • 你把 update 变成 select, 看下执行的结果, 应该能够理解为什么

    对于更新而言, 如果一条记录区别多条, 那就是多次更新, 最终能够留下来的只有一个结果

    你把 else 0, 变成 else 更新的字段, 得到的结果应该是你想要的

    • 已建议为答案 亂馬客 2014年3月24日 10:24
    2014年3月24日 7:16
  • 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


    http://feiyun0112.cnblogs.com/

    2014年3月24日 9:32
  • 可以分批次进行更新
    2014年3月24日 10:11
    版主
  • 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);


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/

    2014年3月24日 10:25