627.Swap Salary-(LeetCode之Database篇)
问题描述
给出下面的表,名为salary。
| id | name | sex | salary |
|---|---|---|---|
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |
要求执行一个UPDATE语句,将表转换成下面的样子。
| id | name | sex | salary |
|---|---|---|---|
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |
即m与f交换位置。
问题解决
下面我使用SQL中的case when来解决问题。
#简单case函数写法
update salary
set sex = (case sex
when 'm' then 'f'
else 'm'
end)
#case搜索函数写法
update salary
set sex = (case
when sex = 'm' then 'f'
else 'm'
end)
这两种方式,可以实现相同的功能。简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式。
进入讨论区后发现很多大神还有其它优秀的解决方案。例如:
UPDATE salary
SET sex = IF(sex='m','f','m')
当表中的某个字段只有两种情况时,可以使用上面的解法。
还有一种解法真是震惊到我了,那就是采用异或的思路。异或有两条规则是:
- 两个相同的数异或为0
- 0与任何数异或还是原数
所以就有了下面的解法:
update salary
set sex = CHAR(ASCII('f') ^ ASCII('m') ^ ASCII(sex))
如果sex为‘m’,三个数做异或后结果结果就为‘f’,这个解法真是相当巧妙。
可以看到这四种方式的运行速度快慢,使用异或的速度是最快的,IF函数的方式速度也还行,不过它只适用于字段只有两种可能的情况。而case when函数是我们最容易想到的,但是其运行速度就稍微的差了点。
627.Swap Salary-(LeetCode之Database篇)的更多相关文章
- 627. Swap Salary
627. Swap Salary SQL Schema Given a table salary, such as the one below, that has m=male and f=femal ...
- 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 数据库带判断的更新操作
https://leetcode.com/problems/swap-salary/description/ 用 set keyWord = Case depentedWord when haha ...
- LeetCode 627. Swap Salary (交换工资)
题目标签: 题目给了我们一个 工资表格,让我们把 男女性别对换. 这里可以利用IF, IF(condition, value_if_true, value_if_false). Java Soluti ...
- [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 ...
- [SQL]LeetCode627. 交换工资 | Swap Salary
SQL架构 create table ), sex ), salary int) Truncate table salary insert into salary (id, name, sex, sa ...
- c++ LeetCode (初级字符串篇) 九道算法例题代码详解(二)
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11089327.html 已经刷了很多篇leetcode题了,不过最近在找c++的实习工作(大佬 ...
- 【持续更新】leetcode算法-数组篇
会在近期陆续地完成数组篇的整理,希望对找工作的小伙伴有所帮助. 1.Two Sum:两数相加为一固定值,求其下标.一次遍历数组,用一个hash表存储已经访问过的数及其下标,对于新访问的数value ...
- LeetCode总结 -- 高精度篇
我们常见的一些主要的数据结构比方整型int或者浮点型float由于位数过多无法用内置类型存储,这时候我们就须要自己实现高精度的数据类型来进行存储和运算.这样的问题在实际产品中还是比較有用的,所以相对来 ...
随机推荐
- jQuery整体架构
(function(global, factory) { factory(global); }(typeof window !== "undefined" ? window : t ...
- 程序员,用NuGet管理好你的包包(转)
每个女人都有很多包包:其实男人也有,但只有会写程序的男人才有 —— 代码世界中的大“包”小“包”.这些大包小包,有花钱买的,有从开源市场淘的,也有自己或同事亲手制作的. 包包有个特点:容易坏,更新快, ...
- Notice: Undefined index: user in D:\phpStudy\WWW\js\ls\lsmc\php\add.php on line 9
原文:Notice: Undefined index: user in D:\phpStudy\WWW\js\ls\lsmc\php\add.php on line 9 (初用数据库(mysql)做用 ...
- Customize Acrylic Brush in UWP Applications(在UWP中自定义亚克力笔刷)
原文 Customize Acrylic Brush in UWP Applications(在UWP中自定义亚克力笔刷) Windows 10 Fall Creators Update(Build ...
- [UWP开发]NavigationView基础使用方法
原文:[UWP开发]NavigationView基础使用方法 [UWP开发]NavigationView基础使用方法 NavigationView是秋季创意者更新(16299)引入的新控件,用于生成W ...
- 获取浏览器的ip以及省份
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> <script type=& ...
- A熟知SP.NET---WebForms UnobtrusiveValidationMode 必须“jquery”ScriptResourceMapping。
我相信,有过ASP.NET人们学习经验RequiredFieldValidator控制(验证非空控制)一定不会陌生,禁止控制输入定义的内容的作用(该属性InitialValue的值.属性默认值为空字符 ...
- WinEdt && LaTex(五)—— 内容的排版
1. 无序列表 需要的环境是\begin{itemize} \end{itemize} \begin{itemize} \item hello \item world \end{itemize} 2. ...
- 嵌入式开发(*(volatile unsigned long *)) 认识
一个.说明 (*(volatile unsigned long *)) 这个语句对于不同的计算机体系结构,设备可能是port映射,也可能是内存映射的. 假设系统结构支持独立的IO地址空间.而且是por ...
- EPPlus简单使用
在使用之前需要在项目中添加对EEPULS.dll的引用 1,创建excel 2,创建sheet 3,添加内容 4,修改 5,保存 FileInfo newFile = new FileInfo(fil ...