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 ...
随机推荐
- 审计日志中的AOP
审计跟踪(也称为审核日志)是一个安全相关的时间顺序记录,记录这些记录的目的是为已经影响在任何时候的详细操作,提供程序运行的证明文件记录.源或事件 MVC 自定义一个过滤器 public class A ...
- php页面zend加密乱码的解决办法
http://www.chinaz.com/program/2008/1021/41485.shtml?qq-pf-to=pcqq.group 今天在服务器部署一个php程序是有zend加密的页面出现 ...
- Mysql Index extends优化
Innodb通过自动把主键列添加到每个二级索引来扩展它们: CREATE TABLE t1 ( i1 , i2 , d DATE DEFAULT NULL, PRIMARY KEY (i1, i2), ...
- React Native学习(八)—— 对接七鱼客服
本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...
- python操作mysql,增,删,改,查
import MySQLdb conn = MySQLdb.connect(host='192.168.1.21',user='yangqw',passwd='1',db='free')cur = c ...
- python_如何让类支持比较运算?
案例: 有时我们希望自定义的类,实例间可以使用比较运算符进行比较,我们自定义比较的行为. 需求: 有一个矩形的类,我们希望比较两个矩形的实例时,比较的是他们的面积 如何解决这个问题? 在类中重新定义比 ...
- python_如何获取文件状态
案例: 在某项目中,需要获取文件状态,如: 文件的类型(普通文件.目录.符合连接.设备文件) 文件的访问权限 文件最后 访问.修改.节点状态 时间 普通文件大小 -- 如何解决? 方法1:通过os原始 ...
- commons-logging的使用
简介 commons-logging是Apache commons类库中的一员.Apache commons类库是一个通用的类库,提供了基础的功能,比如说commons-fileupload,comm ...
- JDBC(四)
1 Apache DBUtils框架 1.1 DBUtils简介 commons-dbutils是Apache组织提供的一个开源JDBC工具类库,它是对JDBC的简单封装,学习成本非常低,并且使用db ...
- 重新认识AWS
Amazon Web Services早期logo: 现在的logo: 一.背景Amazon Web Services,简称AWS.目前世界第一大云计算厂商,AWS 云在全球 18 个地理地区内运营着 ...