小编典典

使用一个查询更新多行?

mysql

我在这里找到了一些可以更新一个字段的东西:http :
//www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-
values-and-a-single-sql-
query/

UPDATE person
    SET name = CASE id
        WHEN 1 THEN 'Jim'
        WHEN 2 THEN 'Mike'
        WHEN 3 THEN 'Precious'
    END
WHERE id IN (1,2,3)

我的问题是如何更新多个字段?如:

UPDATE person
    SET name = CASE, sex = CASE id
        WHEN 1 THEN 'Jim', 'female'
        WHEN 2 THEN 'Mike', 'male'
        WHEN 3 THEN 'Precious', 'male'
    END
WHERE id IN (1,2,3)

这当然是行不通的。尝试了其他几种组合并失败了。任何的想法?谢谢!


阅读 268

收藏
2020-05-17

共1个答案

小编典典

UPDATE person
    SET name = CASE id
        WHEN 1 THEN 'Jim'
        WHEN 2 THEN 'Mike'
        WHEN 3 THEN 'Precious'
    END,
    sex = CASE id
        WHEN 1 THEN 'female'
        WHEN 2 THEN 'male'
        WHEN 3 THEN 'male'
    END
WHERE id IN (1,2,3)
2020-05-17