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的更多相关文章

  1. 【leetcode】Exchange Seats

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

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

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

  3. 626. Exchange Seats-(LeetCode之Database篇)

    问题表述 数据库表如下: id student 1 Abbot 2 Doris 3 Emerson 4 Green 5 Jeames 现在要通过SQL语句将表变换成如下: id student 1 D ...

  4. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  5. Leetcode中的SQL题目练习(二)

    175. Combine Two Tables https://leetcode.com/problems/combine-two-tables/description/ Description Pe ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. 【sql】leetcode习题 (共 42 题)

    [175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...

  8. LeetCode:626.换座位

    题目链接:https://leetcode-cn.com/problems/exchange-seats/ 题目 小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们 ...

  9. 【LeetCode题解】排序

    1. 排序 排序(sort)是一种常见的算法,把数据根据特定的顺序进行排列.经典的排序算法如下: 冒泡排序(bubble sort) 插入排序(insertion sort) 选择排序(selecti ...

随机推荐

  1. parseInt原来是这样用的

    今天在群里无意中看到了这样一个问题,突然发现不会,结果运行一看,懵逼了,不知道为什么???(结果是啥?自己去试试看) 现在我们还是先来复习一下parseInt()这个知识点吧! parseInt() ...

  2. HDU 1232 畅通工程(模板——并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1232 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出 ...

  3. CSS3技巧巧妙使用:not(:last-of-type)简化你的css代码

    终于找到了一个好方法,使用:not(:last-of-type)简单方便,再也不要麻烦的单独使用:last-of-type了,不错! 应用场景:平时我们的列表一般都会有分割线,但是最后一个列表没有分割 ...

  4. 我是如何将网站全站启用Https的?-记录博客安装配置SSL证书全过程

    评论»   文章目录 为什么要Https 如何选择Https 安装部署SSL证书 平滑过渡Https 搜索引擎的响应 启用Https小结 正如大家所看到的,部落全站已经启用了Https访问了,连续几天 ...

  5. linux下卸载apache方法小结

    方法一 代码如下: 1. root@server ~]# rpm -qa|grep httpd  httpd-2.2.3-11.el5_2.centos.4  httpd-manual-2.2.3-1 ...

  6. 邓_phpcms_二次开发_留言板

    ================================================================= •在 phpcms/modules 目录下创建文件夹,并将其命名为g ...

  7. [机器学习]模型评价参数,准确率,召回率,F1-score

    很久很久以前,我还是有个建筑梦的大二少年,有一天,讲图的老师看了眼我的设计图,说:"我觉得你这个设计做得很紧张".当时我就崩溃,对紧张不紧张这样的评价标准理解无能.多年后我终于明白 ...

  8. [原创]消灭eclipse中运行启动的错误:“找不到或无法加载主类”问题

    最近一直遇到这个问题且根据网上的文章做法基本无法通过,故将自己的解决步骤记录及分享给大家. 一:环境必须要配置好. 试试在dos界面输入:java.javac 分别这两个命令是否能执行,如果都能执行恭 ...

  9. html静态页面乱码

    1.将文件保存为UTF-8 2.写入以下代码 <!-- 防止中文乱码 --><meta http-equiv="Content-Type" content=&qu ...

  10. linux 中nvme 的中断申请及处理

    /** * struct irq_desc - interrupt descriptor * @irq_data: per irq and chip data passed down to chip ...