Salary 表:+-------------+----------+| Column Name | Type |+-------------+----------+| id | int || name | varchar || sex | ENUM || salary | int |+-------------+----------+id 是这个表的主键。sex 这一列的值是 ENUM 类型,只能从 ('m', 'f') 中取。本表包含公司雇员的信息。请你编写一个 SQL 查询来交换所有的 'f' 和 'm' (即,将所有 'f' 变为 'm' ,反之亦然),仅使用 单个 update 语句 ,且不产生中间临时表。注意,你必须仅使用一条 update 语句,且 不能 使用 select 语句。查询结果如下例所示。示例 1:输入:Salary 表:+----+------+-----+--------+| 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 |+----+------+-----+--------+解释:(1, A) 和 (3, C) 从 'm' 变为 'f' 。(2, B) 和 (4, D) 从 'f' 变为 'm' 。来源:力扣(LeetCode)链接:https://leetcode.cn/problems/swap-salary
#测试数据Create table If Not Exists Salary (id int, name varchar(100), sex char(1), salary int);insert into Salary (id, name, sex, salary) values ('1', 'A', 'm', '2500');insert into Salary (id, name, sex, salary) values ('2', 'B', 'f', '1500');insert into Salary (id, name, sex, salary) values ('3', 'C', 'm', '5500');insert into Salary (id, name, sex, salary) values ('4', 'D', 'f', '500');
update Salaryset sex = case when sex='m' then 'f' else 'm' end;

笔试题合集免费领取方法
方法一:关注公众号【跟强哥学SQL】,回复关键字【力扣】获取链接。
方法二:访问【SQL网】:https://sql.wang/sql-leetcode/sql-exercise
文章转载自跟强哥学SQL,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




