在 MySQL 中更新 1 列中的多行

发布于 2025-01-07 04:04:06 字数 322 浏览 7 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩物 2025-01-14 04:04:06

您可以使用如下案例:

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

you can use cases like below:

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
青芜 2025-01-14 04:04:06

不适用于您的示例,但您可能会发现这很有用:

UPDATE table
SET value = <value>
WHERE field = <specific value>

通过这种方式,您可以根据同一表中的另一个字段更新表中的一个字段。所有适用的行都将更新。引用我今天早上早些时候在工作中使用的一个示例。

UPDATE porderitems
SET currency = -2
WHERE ord = 40396

此查询更新 porderitems 表(采购订单行),将连接到采购订单 40396 的所有行的货币设置为 -2。该查询既不知道也不关心有多少行。位于该采购订单中;所有这些都将被更新。

Not applicable to your example, but you probably will find this useful:

UPDATE table
SET value = <value>
WHERE field = <specific value>

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

UPDATE porderitems
SET currency = -2
WHERE ord = 40396

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.

静若繁花 2025-01-14 04:04:06

如果您的值来自另一个表:

UPDATE example_table
JOIN values_table ON values_table.id = example_table.id
SET example_table.variable1 = values_table.value

if your values are from another table:

UPDATE example_table
JOIN values_table ON values_table.id = example_table.id
SET example_table.variable1 = values_table.value
初雪 2025-01-14 04:04:06

UPDATE personal_details SET country_id= 6,其中 id 介于 26 和 40 之间。我认为如果新值相同并且需要在多行中更新,则此代码将起作用。

UPDATE personal_details SET country_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文