在 MySQL 中更新 1 列中的多行
在 MySQL 中同时更新多行的正确查询是什么?
我只更新 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;
这看起来可能效率低下,或者如果它是最有效的查询,请告诉我:)
What is the proper query for updating multiple rows in MySQL at the same time?
I am only updating 1 column:
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用如下案例:
you can use cases like below:
不适用于您的示例,但您可能会发现这很有用:
通过这种方式,您可以根据同一表中的另一个字段更新表中的一个字段。所有适用的行都将更新。引用我今天早上早些时候在工作中使用的一个示例。
此查询更新 porderitems 表(采购订单行),将连接到采购订单 40396 的所有行的货币设置为 -2。该查询既不知道也不关心有多少行。位于该采购订单中;所有这些都将被更新。
Not applicable to your example, but you probably will find this useful:
This way you can update one field in a table on the basis of another field in the same table. All the applicable rows will be updated. To quote an example which I used at work earlier this morning
This query update the porderitems table (purchase order lines), setting the currency to -2 for all the lines connected to purchase order 40396. The query neither knows nor cares how many lines there are in that purchase order; all of them will be updated.
如果您的值来自另一个表:
if your values are from another table:
UPDATE
personal_details
SETcountry_id
= 6,其中 id 介于 26 和 40 之间。我认为如果新值相同并且需要在多行中更新,则此代码将起作用。UPDATE
personal_details
SETcountry_id
= 6 where id between 26 and 40. I think this code would work if the new value is same and it needs to update in multiple rows.