更新MySQL中1列中的多行 [英] Update multiple rows in 1 column in MySQL
本文介绍了更新MySQL中1列中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在MySQL中同时更新多行的正确查询是什么?
What is the proper query for updating multiple rows in MySQL at the same time?
我只更新1列:
UPDATE example_table SET variable1 = 12 WHERE id=1;
UPDATE example_table SET variable1 = 42 WHERE id=2;
UPDATE example_table SET variable1 = 32 WHERE id=3;
UPDATE example_table SET variable1 = 51 WHERE id=4;
这似乎效率低下,或者如果它是最有效的查询,请告诉我:)
This seems like it may be inefficient, or if it is the most efficient query let me know :)
推荐答案
您可以使用以下情况:
UPDATE example_table
SET variable1 = CASE id
WHEN 1 THEN 12
WHEN 2 THEN 42
WHEN 3 THEN 32
WHEN 4 THEN 51
END
WHERE id BETWEEN 1 AND 4
这篇关于更新MySQL中1列中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文