LeetCode - 626. Exchange Seats
Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids.
The column id is continuous increment.
Mary wants to change seats for the adjacent students.
Can you write a SQL query to output the result for Mary?
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
For the sample input, the output is:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
Note:
If the number of students is odd, there is no need to change the last one's seat.
# Write your MySQL query statement below
SELECT
s.id,
s.student
FROM
(
SELECT
id - 1 AS id,
student
FROM
seat
WHERE
(id % 2 = 0)
UNION
SELECT
(CASE WHEN (cnt%2=1) AND id=cnt THEN id ELSE id + 1 END) AS id,
student
FROM
seat,
(select count(*) as cnt from seat) as seatcnt
WHERE
(id % 2 = 1)
) s
GROUP BY
s.id ASC
LeetCode - 626. Exchange Seats的更多相关文章
- 【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 ...
- 626. Exchange Seats-(LeetCode之Database篇)
问题表述 数据库表如下: id student 1 Abbot 2 Doris 3 Emerson 4 Green 5 Jeames 现在要通过SQL语句将表变换成如下: id student 1 D ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Leetcode中的SQL题目练习(二)
175. Combine Two Tables https://leetcode.com/problems/combine-two-tables/description/ Description Pe ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【sql】leetcode习题 (共 42 题)
[175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...
- LeetCode:626.换座位
题目链接:https://leetcode-cn.com/problems/exchange-seats/ 题目 小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们 ...
- 【LeetCode题解】排序
1. 排序 排序(sort)是一种常见的算法,把数据根据特定的顺序进行排列.经典的排序算法如下: 冒泡排序(bubble sort) 插入排序(insertion sort) 选择排序(selecti ...
随机推荐
- .24-浅析webpack源码之事件流compilation(2)
下一个compilation来源于以下代码: compiler.apply(new EntryOptionPlugin()); compiler.applyPluginsBailResult(&quo ...
- thinkphp发送邮件需要开启什么设置
邮件配置 'THINK_EMAIL' => array( 'SMTP_HOST' => 'smtp.163.com', //SMTP服务器 'SMTP_PORT' => '465', ...
- dedecms环境优化
路径:dedecms/dede/templates/index_body.htm <script type="text/javascript">function sho ...
- Javascript闭包入门(译文)
前言 总括 :这篇文章使用有效的javascript代码向程序员们解释了闭包,大牛和功能型程序员请自行忽略. 译者 :文章写在2006年,可直到翻译的21小时之前作者还在完善这篇文章,在Stackov ...
- extends和implements的区别
extends表示继承 implements表示抽象类的接口
- xen虚拟机(Centos6.6)的创建和扩容
好久没来更博了,从这篇开始,我准备好好梳理一下Hadoop的那些"事".本人技术渣一枚,只是本着知识共享的原则,希望将我所掌握的知识分享出来.若文中有错误的地方,欢迎大家告知我,我 ...
- linux指令--ls
本篇博客转自http://www.cnblogs.com/peida/archive/2012/10/23/2734829.html,在原作者基础上做了修改和总结. ls命令是linux下最常用的命令 ...
- 【转】How to append current date and timestamp to filename in shell script
$ date +"FORMAT" now=$(date +"%Y-%m-%d-%S") filename="my_program.$now.log&q ...
- SQL语句-INSERT语句
Insert语句 Insert语句三种写法: mysql> desc students; +-------+-------------+------+-----+---------+------ ...
- eclipse每次闪退后都提示查看\workspace\.metadata\.log
错误如下: 找到<workspace>/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi"文件,将其删掉,再重启 ...