当前位置:网站首页>627. Change of gender
627. Change of gender
2022-08-05 02:33:00 【just six z】
前言
Salary 表:
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| id | int |
| name | varchar |
| sex | ENUM |
| salary | int |
+-------------+----------+
id 是这个表的主键.
sex 这一列的值是 ENUM 类型,只能从 ('m', 'f') 中取.
本表包含公司雇员的信息.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/swap-salary
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
请你编写一个 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
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
1、if
update salary set sex = if(sex = 'm','f','m');
2、case + when + else
update salary
set sex = (
case sex when 'm' then 'f' else 'm' end
) ;
3、char + ascii
update salary set sex = char(ascii('m') + ascii('f') - ascii(sex));
边栏推荐
- 网络安全与元宇宙:找出薄弱环节
- HOG特征学习笔记
- What should I do if the self-incrementing id of online MySQL is exhausted?
- 2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
- 如何模拟后台API调用场景,很细!
- 【LeetCode刷题】-数之和专题(待补充更多题目)
- matlab绘制用颜色表示模值大小的箭头图
- 力扣-二叉树的前序遍历、中序遍历、后序遍历
- iNFTnews | What can NFTs bring to the sports industry and fans?
- Greenplum Database Fault Analysis - Why Does gpstart -a Return Failure After Version Upgrade?
猜你喜欢
【OpenCV 图像处理2】:OpenCV 基础知识
matlab绘制用颜色表示模值大小的箭头图
DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
【 2 】 OpenCV image processing: basic knowledge of OpenCV
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
剑指offer专项突击版第20天
Tree search (bintree)
【C语言】详解栈和队列(定义、销毁、数据的操作)
线性表的查找
What should I do if the self-incrementing id of online MySQL is exhausted?
随机推荐
Greenplum Database Fault Analysis - Why Does gpstart -a Return Failure After Version Upgrade?
CPDA|运营人如何从负基础学会数据分析(SQL)
DAY23: Command Execution & Code Execution Vulnerability
力扣-二叉树的前序遍历、中序遍历、后序遍历
C学生管理系统 指定位置插入学生节点
采用redis缓存的linux主从同步服务器图片硬盘满了移到新目录要修改哪些指向
[Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?
LeetCode uses the minimum cost to climb the stairs----dp problem
Pisanix v0.2.0 released | Added support for dynamic read-write separation
Access Characteristics of Constructor under Inheritance Relationship
1873. 计算特殊奖金
Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
View handler stepping record
行业案例|世界 500 强险企如何建设指标驱动的经营分析系统
使用SuperMap iDesktopX数据迁移工具迁移地图文档和符号
数据增强Mixup原理与代码解读
[ROS] (10) ROS Communication - Service Communication
iNFTnews | What can NFTs bring to the sports industry and fans?
1667. 修复表中的名字