CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
END CASE
将salary表中sex字段中m替换为f,f替换为m
mysql> select * from salary;
+------+------+------+--------+
| id | name | sex | salary |
+------+------+------+--------+
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |
+------+------+------+--------+
4 rows in set (0.00 sec)
mysql> update salary set sex = case sex when 'm' then 'f' else 'm' end;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from salary;
+------+------+------+--------+
| id | name | sex | salary |
+------+------+------+--------+
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |
+------+------+------+--------+
4 rows in set (0.00 sec)