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由于位数过多无法用内置类型存储,这时候我们就须要自己实现高精度的数据类型来进行存储和运算.这样的问题在实际产品中还是比較有用的,所以相对来 ...
随机推荐
- 机器学习: t-Stochastic Neighbor Embedding 降维算法 (一)
Introduction 在计算机视觉及机器学习领域,数据的可视化是非常重要的一个应用,一般我们处理的数据都是成百上千维的,但是我们知道,目前我们可以感知的数据维度最多只有三维,超出三维的数据是没有办 ...
- Android实现图片滚动控件,含页签功能,让你的应用像淘宝一样炫起来
首先题外话,今天早上起床的时候,手滑一下把我的手机甩了出去,结果陪伴我两年半的摩托罗拉里程碑一代就这么安息了,于是我今天决定怒更一记,纪念我死去的爱机. 如果你是网购达人,你的手机上一定少不了淘宝客户 ...
- 【原】对MYSQL下视图的一些总结
注:本文使用mysql5.5版本为例. 做过数据库开发的同学,对视图(VIEW)应该不会陌生. 我接触视图最多的应用场景有两个: 1)出于权限问题,为了限制访问者看到过多的表字段(或内容),就 ...
- Windows10内置ubuntu子系统安装后中文环境设置
原文:Windows10内置ubuntu子系统安装后中文环境设置 第一开启相关设置,使用小娜(Win键+c)直接查找关键字打开更快. ①设置→查找"针对开发人员"→开发人员模式 ...
- Windows Presentation Foundation (WPF)中的命令(Commands)简述
原文:Windows Presentation Foundation (WPF)中的命令(Commands)简述 ------------------------------------------- ...
- C#: Get current keyboard layout\input language
原文 https://yal.cc/csharp-get-current-keyboard-layout/ On some occasions, you may want to get a " ...
- sql like N'%...%' 在C#里的写法
StringBuilder sb = new StringBuilder(); List<SqlParameter> parameters =new List<SqlParamete ...
- XF 通过判断平台加载不同的图片
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 通通玩blend美工(6)下——仿iPhone滚动选择器的ListBox(交互逻辑)
原文:通通玩blend美工(6)下--仿iPhone滚动选择器的ListBox(交互逻辑) 上一篇我们已经把界面画出来了,这篇我们就来制作交互的逻辑吧.上一篇的电梯: http://www.cnblo ...
- WPF DataGrid的LoadingRow事件
<Window x:Class="DataGridExam.MainWindow" xmlns="http://schemas.microsoft.c ...