如何同时更新所有 MySQL 表行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6993376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update all MySQL table rows at the same time?
提问by user664168
How do I update all MySQL table rows at the same time?
如何同时更新所有 MySQL 表行?
For example, I have the table:
例如,我有一张桌子:
id | ip | port | online_status |
1 | ip1 | port1 | |
2 | ip2 | port2 | |
3 | ip3 | port3 | |
4 | ip4 | port4 | |
5 | ip5 | port5 | |
I'm planning to create cronjob and monitor some servers, but I don't know exactly how to update them all from the table at the same time. What are some examples on how to do that?
我计划创建 cronjob 并监控一些服务器,但我不知道如何同时从表中更新它们。有什么例子可以说明如何做到这一点?
回答by Bohemian
Omit the where
clause:
省略where
子句:
update mytable set
column1 = value1,
column2 = value2,
-- other column values etc
;
This will give all rows the same values.
这将为所有行提供相同的值。
This might not be what you want - consider truncate
then a mass insert
:
这可能不是您想要的 - 考虑truncate
一下质量insert
:
truncate mytable; -- delete all rows efficiently
insert into mytable (column1, column2, ...) values
(row1value1, row1value2, ...), -- row 1
(row2value1, row2value2, ...), -- row 2
-- etc
;
回答by heximal
update mytable set online_status = 'online'
If you want to assign different values, you should use the TRANSACTION technique.
如果要分配不同的值,则应使用 TRANSACTION 技术。
回答by inorganik
The default null value for a field is "not null". So you must set it to "null" before you can set that field value for any record to null. Then you can:
字段的默认空值是“非空”。因此,您必须先将其设置为“null”,然后才能将任何记录的该字段值设置为 null。然后你可以:
UPDATE `myTable` SET `myField` = null
回答by Kent Allen Sison
You can try this,
你可以试试这个
UPDATE *tableName* SET *field1* = *your_data*, *field2* = *your_data* ... WHERE 1 = 1;
Well in your case if you want to update your online_status to some value, you can try this,
那么在你的情况下,如果你想将你的 online_status 更新为某个值,你可以试试这个,
UPDATE thisTable SET online_status = 'Online' WHERE 1 = 1;
Hope it helps. :D
希望能帮助到你。:D
回答by hari babu
UPDATE dummy SET myfield=1 WHERE id>1;
回答by Vikrant Devda
i just want to add one more way put a condition in where
clause which is always true, for all columns
我只想添加另一种方式,在where
子句中放置一个条件,对于所有列,该条件始终为真
UPDATE tablename SET online_status=0 WHERE 1=1;
and as @bohemian already answered it well, simply just omit the where
clause
由于@bohemian 已经回答得很好,只需省略该where
条款
UPDATE tablename SET online_status=0;
回答by Saravanan Nandhan
Just add parameters, split by comma:
只需添加参数,以逗号分隔:
UPDATE tablename SET column1 = "value1", column2 = "value2" ....
see the link also MySQL UPDATE
另见链接 MySQL UPDATE