626. Exchange Seats-(LeetCode之Database篇)
问题表述
数据库表如下:
| 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篇)的更多相关文章
- LeetCode - 626. Exchange Seats
Mary is a teacher in a middle school and she has a table seat storing students' names and their corr ...
- 搭建IIS CA DC Exchange TMG SQL (CA DC篇)
搭建IIS CA DC Exchange TMG SQL (CA DC篇) 步骤 1: 在“下一步(N) > (按下按钮)”(位于“添加角色向导”中)上用户左键单击 步骤 2: 在“Ac ...
- 【leetcode】Exchange Seats
Mary is a teacher in a middle school and she has a table seat storing students' names and their corr ...
- [SQL]LeetCode626. 换座位 | Exchange Seats
SQL架构 Create table If Not Exists seat(id )) Truncate table seat insert into seat (id, student) value ...
- c++ LeetCode (初级字符串篇) 九道算法例题代码详解(二)
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11089327.html 已经刷了很多篇leetcode题了,不过最近在找c++的实习工作(大佬 ...
- LeetCode总结 -- 高精度篇
我们常见的一些主要的数据结构比方整型int或者浮点型float由于位数过多无法用内置类型存储,这时候我们就须要自己实现高精度的数据类型来进行存储和运算.这样的问题在实际产品中还是比較有用的,所以相对来 ...
- 【持续更新】leetcode算法-数组篇
会在近期陆续地完成数组篇的整理,希望对找工作的小伙伴有所帮助. 1.Two Sum:两数相加为一固定值,求其下标.一次遍历数组,用一个hash表存储已经访问过的数及其下标,对于新访问的数value ...
- leetcode中Database题(一)
Combine Two Tables Table: Person +-------------+---------+ | Column Name | Type | +-------------+--- ...
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
随机推荐
- Redis入门手册
这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下 [本教程目录] 1.redis是什么 2.redis的作者何许人也 3.谁在使用r ...
- 微服务实践之路--RPC
微服务实践之路--RPC 重点来了,本文全面阐述一下我们的RPC是怎么实现并如何使用的,跟Kubernetes和Openstack怎么结合. 在选型一文中说到我们选定的RPC框架是Apache Thr ...
- PL/SQL 异常处理程序
异常处理程序 一个好的程序应该能够妥善处理各种错误情况,并尽可能从错误中恢复.ORACLE 提供异常(EXCEPTION)和异常处理(EXCEPTION HANDLER)错误处理 ①有三种类型的 ...
- [转] css3制作图形大全
Square #square { width: 100px; height: 100px; background: red; } Rectangle #rectangl ...
- WPF Binding妙处-既无Path也无Source
<Window x:Class="XamlTest.Window12" xmlns="http://schemas.microsoft.com/win ...
- c#通过datatable导出excel和word
/// <summary> /// 导出datatable到word /// </summary> /// <param name="dg">需 ...
- gcc/g++编译(生动形象,从最容易入手的hello world解释了库的概念)
1. gcc/g++在执行编译工作的时候,总共需要4步 (1).预处理,生成.i的文件[预处理器cpp] (2).将预处理后的文件不转换成汇编语言,生成文件.s[编译器egcs] (3).有汇编变为目 ...
- Aspect Oriented Programming面向切面编程
I简介 Aspect Oriented Programming(AOP),面向切面编程,是一个比较热门的话题.AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或 ...
- Android 查看Apk签名方式V1和V2
Android 查看Apk签名方式V1和V2 java -jar apksigner.jar verify -v my.apk -- Verifies Verified using v1 scheme ...
- js操作select控件大全(包含新增、修改、删除、选中、清空、判断存在等)
原文:js操作select控件大全(包含新增.修改.删除.选中.清空.判断存在等) js操作select控件大全(包含新增.修改.删除.选中.清空.判断存在等) js 代码// 1.判断select选 ...