3
2016-10-11 20:04:10 +08:00
@ebony0319 ,这样写确实有问题,会默认更新所有行。
由于我当时写的测试表,只有一行,所以下意识的没有想到这一点。
另外, MySQL 会阻止这种语法吗?我看官方文档上的说明是:
The WHERE clause, if given, specifies the conditions that identify which rows to update. With no WHERE clause, all rows are updated.
所有行都会被更新啊。
另外,刚刚整了个 100499 行的表测试了一下,所有行都被更新了啊:
```
mysql> select count(*) from service;
+----------+
| count(*) |
+----------+
| 100499 |
+----------+
1 row in set (0.04 sec)
mysql> desc service;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| ser_id | int(11) | NO | PRI | NULL | auto_increment |
| value | varchar(32) | YES | | NULL | |
+--------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> explain update service set value='abc'\G
*************************** 1. row ***************************
id: 1
select_type: UPDATE
table: service
partitions: NULL
type: index
possible_keys: NULL
key: PRIMARY
key_len: 4
ref: NULL
rows: 100827
filtered: 100.00
Extra: NULL
1 row in set (0.00 sec)
mysql> select now();update service set value='abc';select now();
+---------------------+
| now() |
+---------------------+
| 2016-10-11 20:01:18 |
+---------------------+
1 row in set (0.00 sec)
Query OK, 100499 rows affected (1.48 sec)
Rows matched: 100499 Changed: 100499 Warnings: 0
+---------------------+
| now() |
+---------------------+
| 2016-10-11 20:01:19 |
+---------------------+
1 row in set (0.00 sec)
```