update multi rows using only one sql; 一句 sql update 多行数据

Posted 厚积_薄发

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了update multi rows using only one sql; 一句 sql update 多行数据相关的知识,希望对你有一定的参考价值。

mysql> select * from states;
+----+---------+------------+
| id | state   | population |
+----+---------+------------+
|  1 | Alabama |    4822026 |
|  2 | bbb     |      33444 |
|  3 | Alabama |    4822024 |
|  4 | ccc     |         44 |
|  5 | ddd     |          5 |
+----+---------+------------+
5 rows in set (0.00 sec)


mysql> INSERT INTO states (id, population) VALUES (4, 444), (5, 555) ON DUPLICATE KEY UPDATE id=values(id), population=values(population);
Query OK, 4 rows affected (0.00 sec)
Records: 2  Duplicates: 2  Warnings: 0


mysql> select * from states;                                                                                                      +----+---------+------------+
| id | state   | population |
+----+---------+------------+
|  1 | Alabama |    4822026 |
|  2 | bbb     |      33444 |
|  3 | Alabama |    4822024 |
|  4 | ccc     |        444 |
|  5 | ddd     |        555 |
+----+---------+------------+

以上是关于update multi rows using only one sql; 一句 sql update 多行数据的主要内容,如果未能解决你的问题,请参考以下文章