MySQL:如果连续更新一个字段两次,结果是?


#MySQL 笔记


结论是:依次执行。

示例

准备数据

CREATE TABLE IF NOT EXISTS `tb_test`(
   `id` INT AUTO_INCREMENT,
   `num` INT NOT NULL,
   PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

insert into tb_test(num) values(1);

更新示例

MySQL root@localhost:mysql> select * from tb_test;
+----+-----+
| id | num |
+----+-----+
| 1  | 1   |
+----+-----+

MySQL root@localhost:mysql> update tb_test set num = 1, num =2 where id = 1;
Query OK, 1 row affected
Time: 0.005s

MySQL root@localhost:mysql> select * from tb_test;
+----+-----+
| id | num |
+----+-----+
| 1  | 2   |
+----+-----+
1 row in set
Time: 0.005s
MySQL root@localhost:mysql> update tb_test set num = 2, num =1 where id = 1;
Query OK, 1 row affected
Time: 0.004s

MySQL root@localhost:mysql> select * from tb_test;
+----+-----+
| id | num |
+----+-----+
| 1  | 1   |
+----+-----+
MySQL root@localhost:mysql> update tb_test set num = 1, num = num + 1 where id = 1;
Query OK, 1 row affected
Time: 0.003s

MySQL root@localhost:mysql> select * from tb_test;
+----+-----+
| id | num |
+----+-----+
| 1  | 2   |
+----+-----+
1 row in set
Time: 0.005s
MySQL root@localhost:mysql> update tb_test set num = num + 1, num =20 where id = 1;
Query OK, 1 row affected
Time: 0.004s

MySQL root@localhost:mysql> select * from tb_test;
+----+-----+
| id | num |
+----+-----+
| 1  | 20  |
+----+-----+
1 row in set
Time: 0.005s


( 本文完 )