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. springcloud干活之服务消费者(feign)

    springcloud系列文章的第三篇 本章将继续讲述springcloud的消费者(feign) Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端. ...

  2. 迈向c++的一次尝试

    从C到C++说着容易做起来也不难,今天做一下尝试. ★:题目介绍:今天是一次尝试所以先从简单的题开始. ★:试题分析:由题可了解到本题目的是要做到由一个数字到一个字符串的转变. 题目简单是由于它只是让 ...

  3. phpmyadmin 自动登录的办法

    在本地开发php项目中,需要配合使用mysql在线管理系统phpmyadmin,因为经常使用,就不想每次都输入密码,所以想办法把用户名密码写入配置文件中,让每次都可以自动登录. 工具/原料   代码编 ...

  4. LAMP环境跟LNMP环境有什么不同,主要用什么地方

    LAMP即Linux+Apache+Mysql/MariaDB+Perl/PHP/Python Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一组常用来搭建动态网 ...

  5. excel去除空格

    =SUBSTITUTE(Sheet1!A1," ","") 在sheet2中输入上述公式,然后再通过拖拽复制公式,即可以很快的将sheet1中的数据全部处理.

  6. apache (order allow,deny or deny,allow)

    平台:"rhel6.2" 实验内容: "测试apache‘order allow,deny’ or ‘order deny,allow’ 功能" 配置文件:&q ...

  7. 洛谷P1233 [木棍加工]

    主要思路: 这道题一眼看过去就可以贪心.. 首先可以按L排序.. 显然排序之后我们就可以抛开L不管了.. 然后就可以愉快的贪心了.. 细节: 这道题可以看成用 最少的合法序列(详见原题) 装下所有木棍 ...

  8. Python中变量和常量的理解

    一.变量的定义:把程序运算的中间结果临时存到内存里,以备后面的代码继续调用,这几个名字的学名就叫做"变量". 二.变量的作用:变量用于存储要在计算机程序中引用和操作的信息.它提供了 ...

  9. Linux指令--touch

    原文出处:http://www.cnblogs.com/peida/archive/2012/10/30/2745714.html linux的touch命令不常用,一般在使用make的时候可能会用到 ...

  10. 【转】sed单行命令大全

    文本间隔:  # 在每一行后面增加一空行  sed G # 将原来的所有空行删除并在每一行后面增加一空行.  # 这样在输出的文本中每一行后面将有且只有一空行.  sed '/^$/d;G' # 在每 ...