当前位置:网站首页>[SQL简单题] LeetCode 627. 变更性别
[SQL简单题] LeetCode 627. 变更性别
2022-07-27 00:21:00 【哇咔咔负负得正】
LeetCode 627. 变更性别
https://leetcode.cn/problems/swap-salary/
请你编写一个 SQL 查询来交换所有的 'f' 和 'm' (即,将所有 'f' 变为 'm' ,反之亦然),仅使用 单个 update 语句 ,且不产生中间临时表。
注意,你必须仅使用一条 update 语句,且 不能 使用 select 语句。
输入:
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 |
+----+------+-----+--------+
Solution
加减法
UPDATE Salary SET sex = char(ascii('m') + ascii('f') - ascii(sex));
异或法
UPDATE Salary SET sex = char(ascii(sex) ^ ascii('m') ^ ascii('f'));
if 函数判断语句
UPDATE Salary SET sex = IF(sex='f', 'm', 'f');
case-when-then-else-end 语句
UPDATE Salary SET
sex =
CASE sex
WHEN 'm' THEN 'f'
WHEN 'f' THEN 'm'
ELSE 'else'
END;
边栏推荐
- Ansible series: do not collect host information gather_ facts: False
- QT编译出来的exe以管理员权限启动
- ZJCTF_login
- Talk about connection pools and threads
- Debezium系列之:记录从库服务器挂掉后binlog文件无法恢复,任务切换到主库并保证数据不丢失的方法
- 基于GoLang实现API短信网关
- 关于url编解码应该选用的函数
- vs2019 中编译和使用 protobuf 库
- Non global function of lua function
- 次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
猜你喜欢

Function stack frame explanation

Interview shock 68: why does TCP need three handshakes?

Database read-write separation and database and table segmentation

智能指针shared_ptr、unique_ptr、weak_ptr

Getlocation:fail the API need to be declared in the requiredprivateinfo field in app.json

If you want to thoroughly optimize the performance, you must first understand the underlying logic~

vs2019 中编译和使用 protobuf 库

聊聊连接池和线程

从ACL 2022 Onsite经历看NLP热点

JMeter interface test, quickly complete a single interface request
随机推荐
"Software testing" packaging resume directly improves the pass rate from these points
贪心——376. 摆动序列
pyqt5使用pyqtgraph画动态散点图
Arduino UNO +74HC164流水灯示例
【RYU】安装RYU常见问题及解决办法
ArduinoUNO驱动RGB模块全彩效果示例
I heard that you knelt on the interface test during the interview?
Greed - 376. Swing sequence
快速排序(Quick sort)
系统安全测试要怎么做,详细来说说
软件测试相关试题知识点
批量复制宝贝上传提示乱码,如何解决?
Information collection port scanning tool nmap instructions
White box test case design (my grandfather can understand it)
C language program compilation
C语言程序的编译(预处理)下
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
如何使用DevExpress WPF在WinUI中创建第一个MVVM应用程序?
Web3.0世界知识体系分享-什么是Web3.0
Plato Farm全新玩法,套利ePLATO稳获超高收益