题目:
给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求只使用一个更新(Update)语句,并且没有中间的临时表。
注意,您必只能写一个 Update 语句,请不要编写任何 Select 语句。
例如:
id | name | sex | salary |
---|---|---|---|
1 | A | m | 2500 |
2 | B | f | 1500 |
3 | C | m | 5500 |
4 | D | f | 500 |
运行你所编写的更新语句之后,将会得到以下表:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/swap-salary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题:
update salary
set sex = (
case when sex = 'm' then 'f' else 'm' end
)
里面的知识点:
1,update 的使用:
update table 表名
set 字段名1=数据1 或表达式1, 字段名2=数据2 或表达式2
[where …=…];
2,case when 语句的使用:
(相当于程序设计语言中的 if else 语句)
case when 的 条件表达式写法
case
when 表达式1 then 返回值 1
when 表达式 2 then 返回值2
else 返回值3
end