Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update all MySQL table rows at the same time?

Tags:

mysql

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?

like image 300
user664168 Avatar asked Aug 09 '11 08:08

user664168


People also ask

How can I update values all at once in MySQL?

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.

How do I update all rows?

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.


1 Answers

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 ;  
like image 120
Bohemian Avatar answered Oct 02 '22 09:10

Bohemian