0

我正在使用 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;

任何人都知道我该如何解决这个问题。

4

3 回答 3

0

尝试typeCase.

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;
于 2012-08-06T09:06:53.347 回答
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 不为空

于 2012-08-06T09:17:57.787 回答
0

查询看起来不错。建议使用 select 来尝试是否会返回任何记录。

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
于 2012-08-06T09:22:22.297 回答