我找到了一些可以在这里更新一个字段的内容: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)
当然不起作用。尝试了其他一些组合并失败了。任何的想法?谢谢!
答案 0 :(得分:42)
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)
答案 1 :(得分:7)
你尝试过类似下面的内容吗?每列可以有一个“SET”语句。
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)