1、单表更新
(1)mysql> SELECT * FROM users;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 25 | 1 |
| 2 | John | 456 | 25 | 1 |
| 3 | Tom | 123 | 25 | 1 |
| 4 | John | 456 | 16 | 1 |
| 5 | John | 456 | 10 | 1 |
| 10 | John | 456 | 16 | 1 |
| 11 | ROSE | 67 | 10 | 0 |
| 12 | Ben | 456 | 10 | NULL |
+----+----------+----------+-----+------+
mysql> UPDATE users SET age = age +5; #更新一列
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 30 | 1 |
| 2 | John | 456 | 30 | 1 |
| 3 | Tom | 123 | 30 | 1 |
| 4 | John | 456 | 21 | 1 |
| 5 | John | 456 | 15 | 1 |
| 10 | John | 456 | 21 | 1 |
| 11 | ROSE | 67 | 15 | 0 |
| 12 | Ben | 456 | 15 | NULL |
+----+----------+----------+-----+------+
(2)更新多列
mysql> UPDATE users SET age = age -id, sex = 0;
mysql> SELECT * FROM users;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 29 | 0 |
| 2 | John | 456 | 28 | 0 |
| 3 | Tom | 123 | 27 | 0 |
| 4 | John | 456 | 17 | 0 |
| 5 | John | 456 | 10 | 0 |
| 10 | John | 456 | 11 | 0 |
| 11 | ROSE | 67 | 4 | 0 |
| 12 | Ben | 456 | 3 | 0 |
+----+----------+----------+-----+------+
(3)加限制条件
mysql> UPDATE users SET age = age +10 WHERE id % 2 = 0;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 29 | 0 |
| 2 | John | 456 | 38 | 0 |
| 3 | Tom | 123 | 27 | 0 |
| 4 | John | 456 | 27 | 0 |
| 5 | John | 456 | 10 | 0 |
| 10 | John | 456 | 21 | 0 |
| 11 | ROSE | 67 | 4 | 0 |
| 12 | Ben | 456 | 13 | 0 |
+----+----------+----------+-----+------+