4/14日SQL语句:
今天遇到的是一个修改语句,这之前没有见过,在此记录一下:
题目:
给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。
https://leetcode-cn.com/problems/swap-salary/
我们先复习一下update语句:
update 表名
set 列名 = 修改后的值;
然后看题目中给的条件,我们可以用选择语句case:
case 列名
when a then b #将a换为b
when c then d #将c换为d
else f #剩下换为f
end
于是代码我们可以改为:
update salary
set sex=(
case sex
when 'm' then 'f'
else 'm'
end
);
即可