Example:
id | name | sex | salary |
---|---|---|---|
1 | A | m | 2500 |
2 | B | f | 1500 |
3 | C | m | 5500 |
4 | D | f | 500 |
After running your update statement, the above salary table should have the following rows:
id | name | sex | salary |
---|---|---|---|
1 | A | f | 2500 |
2 | B | m | 1500 |
3 | C | f | 5500 |
4 | D | m | 500 |
使用 sql swap 表中的 sex 字段,不能用select,是能使用 update
update salary set sex = IF(sex = 'm', 'f', 'm');
update salary set sex = CHAR(ASCII('f') ^ ASCII('m') ^ ASCII(sex));
update salary set sex = (case when sex='m' then 'f' when sex='f' then 'm' end)