[LeetCode] 627.交换性别
给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。
要求只使用一个更新(Update)语句,并且没有中间的临时表。
注意,您必只能写一个 Update 语句,请不要编写任何 Select 语句。
例如:
| 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 |
题解:
方法一:使用 CASE...WHEN... 流程控制语句
UPDATE salary
SET
sex = CASE sex
WHEN 'm' THEN 'f'
ELSE 'm'
END;
方法二:使用字母和ASCII互转
UPDATE salary
Set
sex = char(ascii('m') + ascii('f') - ascii(sex));
方法三:使用if
UPDATE salasy
Set
sex = if (sex = 'f','m','f');
摘自:https://leetcode-cn.com/problems/swap-salary/solution/jiao-huan-gong-zi-by-leetcode/
[LeetCode] 627.交换性别的更多相关文章
- SQL语句中IF的简单使用 - 关联leetcode 627.交换工资
MySQL的IF既可以作为表达式用,也可在存储过程中作为流程控制语句使用,如下是做为表达式使用: IF表达式 IF(expr1,expr2,expr3) 如果 expr1 是TRUE (expr1 & ...
- LeetCode 627. Swap Salary (交换工资)
题目标签: 题目给了我们一个 工资表格,让我们把 男女性别对换. 这里可以利用IF, IF(condition, value_if_true, value_if_false). Java Soluti ...
- LeetCode:627.交换工资
题目链接:https://leetcode-cn.com/problems/swap-salary/ 题目 给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值.交换所有的 ...
- “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer ...
- leetcode 627. Swap Salary 数据库带判断的更新操作
https://leetcode.com/problems/swap-salary/description/ 用 set keyWord = Case depentedWord when haha ...
- leetcode 5199. 交换字符串中的元素
地址 https://leetcode-cn.com/contest/weekly-contest-155/problems/smallest-string-with-swaps/ 给你一个字符串 ...
- LeetCode - 627. Swap Salary
Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m v ...
- [LeetCode] 627. Swap Salary_Easy tag: SQL
Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m v ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- C#设计模式:桥接模式(Bridge Pattern)
一,桥接模式,直接上代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...
- SIP UserAgent (B2BUA client)——libosip2 libeXosip2
1. libosip2 libeXosip2 http://www.antisip.com/download/exosip2/ Features:--------eXosip2 has support ...
- Nginx学习总结:常用module(二)
斜体下划线,表示建议采用默认配置,无需显式的配置 一.ngx_core_module 1.accept_mutex [on | off] 上下文:events 默认为“on”,在wor ...
- Spring Boot 2 Webflux的全局异常处理
https://www.jianshu.com/p/6f631f3e00b9 本文首先将会回顾Spring 5之前的SpringMVC异常处理机制,然后主要讲解Spring Boot 2 Webflu ...
- How To Find Out Attachments By File Type In Outlook?
ext: (extension extension) Take the attachments of zip files and of txt files for example, just ente ...
- 设备树中#address-cells和#size-cells作用
device tree source Example1 / { #address-cells = <0x1>; // 在 root node 下使用 1 個 u32 來代表 address ...
- day17 python re模块 简易爬虫
day17 python 一.re模块 1.re模块的基础方法 查找findall() import re #re.findall(pattern,string,flags ...
- 25.Java锁的深度化
Java锁的深度化 悲观锁.乐观锁.排他锁 场景 当多个请求同时操作数据库时,首先将订单状态改为已支付,在金额加上200,在同时并发场景查询条件下,会造成重复通知. SQL: Update 悲观锁与乐 ...
- MySQL-其它整理
来自:http://www.w3school.com.cn/sql/sql_server.asp 一:基本操作 1)插入 INSERT INTO 表名称 VALUES (值1, 值2,....): I ...
- pgsql SQL监控,查询SQL执行情况
SELECT procpid, START, now() - START AS lap, current_query FROM ( SELECT backendid, pg_stat_get_back ...