问题表述


数据库表如下:

id student
1 Abbot
2 Doris
3 Emerson
4 Green
5 Jeames

现在要通过SQL语句将表变换成如下:

id student
1 Doris
2 Abbot
3 Green
4 Emerson
5 Jeames

即id不变,奇数位和偶数位交换位置,如果表的总行数为奇数,则最后一行不变。

问题解决


首先看到这个问题,我就想SQL里面是不是有什么置换函数之类的,结果去查了查,并没有这样的函数。在我尝试了各种select方法后,还是没能将这题解出来…最后还是去讨论区看了看大神们的解答,看完各种答案后瞬间豁然开朗。

这题的解题思路其实并不是想办法将student的列置换,而是通过操作id列来达到置换的效果。

比较通过的解法就是下面这种:

select
if(id < (select count(*) from seat), if(id mod 2=0, id-1, id+1), if(id mod 2=0, id-1, id)) as id, student
from seat
order by id asc

其中IF函数的用法:

格式:IF(Condition,A,B)

意义:当Condition为TRUE时,返回A;当Condition为FALSE时,返回B。

所以上面的SQL语句就是,先对id进行操作。先计算总行数,最后一行如果是奇数id不变,如果是偶数id减1,其余行id为奇数的让id加1,id为偶数的让id减1,最后再对id做升序操作,就可以得到结果了。

其中还有一种解法:

/* get all the even numbered rows as odd numbered rows */
SELECT s1.id - 1 as id, s1.student
FROM Seat s1
WHERE s1.id MOD 2 = 0 UNION /* get all the odd numbered rows as even numbered rows */
SELECT s2.id + 1 as id, s2.student
FROM Seat s2
WHERE s2.id MOD 2 = 1 AND s2.id != (SELECT MAX(id) FROM Seat)
/* Just don't get the last row as we will handle it in the next UNION */ UNION /* get the last row if odd and don't change the id value */
SELECT s3.id, s3.student
FROM Seat s3
WHERE s3.id MOD 2 = 1 AND s3.id = (SELECT MAX(id) FROM Seat) /* Order the result by id */
ORDER BY id ASC;

思路都和第一种方法大同小异。

626. Exchange Seats-(LeetCode之Database篇)的更多相关文章

  1. LeetCode - 626. Exchange Seats

    Mary is a teacher in a middle school and she has a table seat storing students' names and their corr ...

  2. 搭建IIS CA DC Exchange TMG SQL (CA DC篇)

    搭建IIS CA DC Exchange TMG SQL (CA DC篇)   步骤 1: 在“下一步(N) > (按下按钮)”(位于“添加角色向导”中)上用户左键单击   步骤 2: 在“Ac ...

  3. 【leetcode】Exchange Seats

    Mary is a teacher in a middle school and she has a table seat storing students' names and their corr ...

  4. [SQL]LeetCode626. 换座位 | Exchange Seats

    SQL架构 Create table If Not Exists seat(id )) Truncate table seat insert into seat (id, student) value ...

  5. c++ LeetCode (初级字符串篇) 九道算法例题代码详解(二)

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11089327.html 已经刷了很多篇leetcode题了,不过最近在找c++的实习工作(大佬 ...

  6. LeetCode总结 -- 高精度篇

    我们常见的一些主要的数据结构比方整型int或者浮点型float由于位数过多无法用内置类型存储,这时候我们就须要自己实现高精度的数据类型来进行存储和运算.这样的问题在实际产品中还是比較有用的,所以相对来 ...

  7. 【持续更新】leetcode算法-数组篇

    会在近期陆续地完成数组篇的整理,希望对找工作的小伙伴有所帮助.   1.Two Sum:两数相加为一固定值,求其下标.一次遍历数组,用一个hash表存储已经访问过的数及其下标,对于新访问的数value ...

  8. leetcode中Database题(一)

    Combine Two Tables Table: Person +-------------+---------+ | Column Name | Type | +-------------+--- ...

  9. LeetCode之Easy篇 ——(7)Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...

随机推荐

  1. Android真机调试不打印日志解决

    1.在拨号界面输入:*#*#2846579#*#* 进入测试菜单界面. 2.Project Menu–后台设置–LOG设置 3.LOG开关–LOG打开 LOG级别设置–VERBOSE 4.Dump&a ...

  2. visual studio 编译器在辨异 C/C++ 程序时的注意事项

    1. 数组大小的限制 visual studio 对数组的维数(元素的个数)没有限制,但要求数组的 size (sizeof() 后的结果,所占内存的大小)不得超过 0x7fff ffff = 2^3 ...

  3. EMES信息化制造系统的概念

    EMES是指,信息化制造系统.它由五个功能子系统构成,分别是:企业信息平台子系统(EIP),物料实时信息系统(MRI).能源实时信息系统(PRI).设备实时信息系统(EMR).质量实时信息系统(QRI ...

  4. zedboard之GPIO驱动器(离FPGA直到LINUX申请书)

    笔者:xiabodan   资源: http://blog.csdn.net/xiabodan/article/details/24308373 1 EDK 大家知道我们在EDK中建立GPIO然后倒出 ...

  5. Leetcode 319 Bulb Switcher 找规律

    有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...

  6. Mac版Visual Studio预览版

    来了,Mac版Visual Studio预览版开放下载 投递人 itwriter 发布于 2016-11-17 12:11 评论(7) 有1317人阅读 原文链接 [收藏] « » 微软前俩天宣布,推 ...

  7. WPF 关于圆角的制作

    原文:WPF 关于圆角的制作 1.使用Boder(一般情况): 设置CornerRadius属性 <Border x:Name="border" CornerRadius=& ...

  8. C# TSF 输入法的获取

    原文 C# TSF 输入法的获取 起因: 「添雨跟打器」中存在一个问题.在 windows 8/10 里面,输入法就获取不到了.我一直没有去管这样的问题.但是也大致知道,可能是 TSF 架构的问题. ...

  9. jquery对象及页面加载完成写法

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. WPF 数据模板DataType属性的使用,不用指定ItemTemplate

    <Window x:Class="CollectionBinding.MainWindow"        xmlns="http://schemas.micros ...