当前位置:网站首页>[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;
边栏推荐
- CS224W fall 1.2 Applications of Graph ML
- As for the pit saved by serialized variables, the data added with indexer cannot be serialized
- 测试人需要的数据库知识:MySQL常用语法
- com.fasterxml.jackson.databind.exc.InvalidDefinitionException
- Arduino UNO +74HC164流水灯示例
- Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
- Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
- Debezium series: the binlog file cannot be recovered after the record is hung from the library server, and the task is switched to the main library to ensure that the data is not lost
- ZJCTF_login
- iNFTnews | “流量+体验”白衬e数字时装节引领数字时装新变迁
猜你喜欢

Redis installation and operation (Linux)

云开发寝适闹钟微信小程序源码

解决小程序报错getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json

哈希表与一致性哈希的原理理解以及应用

Cloud development sleeping alarm clock wechat applet source code

Uni app wechat applet search keywords are displayed in red

setTimeout第一个参数应该注意的地方

Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验

Kubernetes dashboard deployment application and access

Inftnews | ggac and China Aerospace ases exclusively produce "China 2065 Collection Edition"
随机推荐
Debezium series: the binlog file cannot be recovered after the record is hung from the library server, and the task is switched to the main library to ensure that the data is not lost
pyqt5使用pyqtgraph画动态散点图
Play a parallel multithreaded mcu-mc3172
[nisactf 2022] upper
ZJCTF_login
从ACL 2022 Onsite经历看NLP热点
Goatgui invites you to attend a machine learning seminar
Arduino UNO +74HC164流水灯示例
OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
智能指针shared_ptr、unique_ptr、weak_ptr
Heisei thousand words (Heisei, September 10, 2012) (Shidu Mingzuo) the universe is far away, the Milky way is permanent, the sun and moon are running disorderly, the earth is open, the seasons shift,
Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
GoatGui邀你参加机器学习研讨班
Kubeadmin到底做了什么?
Arduinouno drive RGB module full color effect example
Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换
阿里云技术专家杨泽强:弹性计算云上可观测能力的构建
Leetcode skimming -- no.238 -- product of arrays other than itself
Talk about connection pools and threads
Interrupt, signal, system call