手记

(LeetCode627:交换工资)


例如:

| id | name | sex | salary |
|----|------|-----|--------|| 1  | A    | m   | 2500   |
| 2  | B    | f   | 1500   || 3  | C    | m   | 5500   |
| 4  | D    | f   | 500    |

运行你所编写的查询语句之后,将会得到以下表:

| id | name | sex | salary |
|----|------|-----|--------|| 1  | A    | f   | 2500   |
| 2  | B    | m   | 1500   || 3  | C    | f   | 5500   |
| 4  | D    | m   | 500    |

Solution:
方式一:

update salary set sex = if(sex='f','m','f');

使用mysql if 表达式

IF(expr1,expr2,expr3)

if expr1 为true,则if()表达式的值为 expr2,否则if()表达式的值为expr2

方式二:

update salart set sex = (case when sex='f' then 'm' else 'f' end);

使用mysql的case when 函数



作者:lconcise
链接:https://www.jianshu.com/p/360af6a2338c


0人推荐
随时随地看视频
慕课网APP