How do I update all MySQL table rows at the same time?
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?
To update values in multiple columns, you use a list of comma-separated assignments by supplying a value in each column's assignment in the form of a literal value, an expression, or a subquery. Third, specify which rows to be updated using a condition in the WHERE clause. The WHERE clause is optional.
Syntax: UPDATE table_name SET column_name1 = new_value1, column_name2 = new_value2 ---- WHERE condition; Here table_name is the name of the table, column_name is the column whose value you want to update, new_value is the updated value, WHERE is used to filter for specific data.
Omit the where
clause:
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 mytable; -- delete all rows efficiently insert into mytable (column1, column2, ...) values (row1value1, row1value2, ...), -- row 1 (row2value1, row2value2, ...), -- row 2 -- etc ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With