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 ...
随机推荐
- UE4 Xml读写
UE4自带一个XmlParser,可以很方便的实现Xml的读写. 1,在PublicDependencyModuleNames.AddRange中添加XmlParser. 2,include XmlP ...
- SpringBoot Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
SpringBoot Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFact ...
- tomcat服务器一闪而过解决方法
JDK没有配置,下载JDK安装到电脑上,然后在电脑->属性->高级系统设置->环境变量,将JDK中bin文件的目录E:\Program Files (x86)\Java\jre7\b ...
- dedecms织梦网站图片集上传图片出现302错误图片提示怎么解决 已测
时间:2016-01-20 来源:未知 作者:模板之家 阅读:次 小编今天上传织梦网站模板的时候,在图片集里面选择上传图片的时候,弹出302错误提示,当是真的是郁闷了,试了好几次,开始还以为是图片过大 ...
- DEDECMS去掉自动生成首页或栏目后面带的index.html
Dede默认生成首页后,首页的链接后面会多出一个index.html.据官方说法这样有利于网站优化.但是这个index.html怎么看都不舒服,而且也不利于seo中主页url的统一.因为我的网站的ur ...
- IOS UI 滚动视图 UIScrollView
UIScrollView 常用属性 scrollView.maximumZoomScale= 2.0; // 缩放最大比例 scrollView.minimumZoomScale = 0.2;// ...
- 基础数据类型的补充和深浅copy
一:关于str 的操作方法补充 1,s.isspace() 判断字符串是否只由空格组成,是,为True,否则,为False. s = ' ' #只能是以至少一个空格组成的字符串(全空格) prin ...
- MyCat 启蒙:分布式系统的数据库架构演变
文章首发于[博客园-陈树义],点击跳转到原文<MyCat 启蒙:分布式系统的数据库架构演变> 单数据库架构 一个项目在初期的时候,为了尽可能快地验证市场,其对业务系统的最大要求是快速实现. ...
- ASPNET 5 和 dnx commands
DNX项目是用来创建和运行.net应用程序适用于windows,mac 和linux 的,dnx提供了一个宿主进程(a host process),CLR托管逻辑( CLR hosting logic ...
- navicat的简单使用
navicat的简单使用: 连接: 输入ip地址,端口,用户名,密码 新建数据库: 数据库名,字符编码一定要选择utf-8 新建表: 字段,约束条件 双击表名,自己打开表,点击空列,添加数据,ctl+ ...