Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

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

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?

question from:https://stackoverflow.com/questions/6993376/how-to-update-all-mysql-table-rows-at-the-same-time

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
; 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...