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. EC+VO+SCOPE for ES3

    词法环境 词法作用域 词法作用域(lexcical scope).即JavaScript变量的作用域是在定义时决定而不是执行时决定,也就是说词法作用域取决于源码. 词法环境 用于定义特定变量和函数标识 ...

  2. 使用django建博客时遇到的URLcon相关错误以及解决方法。错误提示:类型错误:include0获得一个意外的关键参数app_name

    root@nanlyvm:/home/mydj/mysite# python manage.py runserver Performing system checks... Unhandled exc ...

  3. python原始字符串

    str1='let's go' File "<input>", line 1 str1='let's go' ^ SyntaxError: invalid syntax ...

  4. JavaScript常见封装方法

    1.最简单的,使用变量,然后用匿名函数包裹,不封装 2.对象字面量简单封装(不完整的模块模式,因为无法达到变量.方法私有效果.不过确实有分离和组织代码的能力,也就算一种简略的模块模式的实现方式) va ...

  5. 【编程技巧】alert vs Ext.Msg.alert

    alert会阻塞程序的运行. Ext.Msg.alert是异步的,它的调用并不会停止浏览器中代码的执行.

  6. MyBatis 查询示例

    环境搭建 数据库schema 1)datasource.xml配置 <?xml version="1.0" encoding="UTF-8"?> & ...

  7. linux nvme的那些workqueue

    目前nvme三个常见的使用的workqueue ,主要有nvme_workq,nvme_rdma_wq ,nvme_fc_wq,下面一一描述一下初始化及使用的场景.分别对应于NVME over PCI ...

  8. python 序列

    序列 序列是python中的一种数据结构,这种数据结构根据索引来获取序列中的对象 有6种内建序列类:list,tuple,string,unicode,buffer,xrange. 其中xrange比 ...

  9. npm install安装时忘记--save解决方法

    title: npm install安装时忘记--save解决方法 date: 2017-05-07 20:17:54 tags: npm categories: --- 网上还有一个解决方案就是: ...

  10. 机器学习-GBDT和XGboost

    参考: 陈天奇slides :   https://homes.cs.washington.edu/~tqchen/pdf/BoostedTree.pdf Friedman GBDT 论文:  htt ...