我正在使用MySQL中的一个案例来尝试此更新语句,但它不会更改任何值。
查询没有给出任何错误,但有0个受影响的行。
UPDATE `tablename`
SET `type` = (
CASE
WHEN `type` = 1 THEN 1
WHEN `type` = 2 THEN 2
WHEN `type` = 3 THEN 19
WHEN `type` = 4 THEN null
WHEN `type` = 5 THEN null
WHEN `type` = 6 THEN 81
WHEN `type` = 7 THEN null
WHEN `type` = 8 THEN 22
WHEN `type` = 9 THEN 21
WHEN `type` = 10 THEN 78
WHEN `type` = 11 THEN 80
WHEN `type` = 12 THEN 79
END)
WHERE user_id IS NOT NULL;
任何人都知道如何解决这个问题。
答案 0 :(得分:0)
尝试在type
之后移动Case
。
UPDATE `tablename`
SET `type` =
CASE `type`
WHEN 1 THEN 1
WHEN 2 THEN 2
WHEN 3 THEN 19
WHEN 4 THEN null
WHEN 5 THEN null
WHEN 6 THEN 81
WHEN 7 THEN null
WHEN 8 THEN 22
WHEN 9 THEN 21
WHEN 10 THEN 78
WHEN 11 THEN 80
WHEN 12 THEN 79
END
WHERE user_id IS NOT NULL;
答案 1 :(得分:0)
UPDATE test
SET `password` = (
CASE
WHEN `password` = 1 THEN 'one'
WHEN `password` = 2 THEN 'two'
WHEN `password` = 3 THEN 'three'
WHEN `password` = 4 THEN null
WHEN `password` = 5 THEN null
WHEN `password` = 6 THEN 81
WHEN `password` = 7 THEN null
END)
where userindex=1
对我来说工作得非常好。我设计它来装配我的测试数据..再次重新设定值并再试一次。
还要检查是否有任何用户不是user_id
答案 2 :(得分:0)
查询看起来很好。建议使用select来尝试if将返回任何记录。
SELECT *, CASE
WHEN `type` = 1 THEN 1
WHEN `type` = 2 THEN 2
WHEN `type` = 3 THEN 19
WHEN `type` = 4 THEN null
WHEN `type` = 5 THEN null
WHEN `type` = 6 THEN 81
WHEN `type` = 7 THEN null
WHEN `type` = 8 THEN 22
WHEN `type` = 9 THEN 21
WHEN `type` = 10 THEN 78
WHEN `type` = 11 THEN 80
WHEN `type` = 12 THEN 79
END as test
FROM `tablename` WHERE user_id IS NOT NULL