2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_626.sql 1.18 KB
一键复制 编辑 原始数据 按行查看 历史
Charies Gavin 提交于 2020-02-06 12:44 . 初始化 myleetcode 项目
-- 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 id,
case
when id%2 = 0 then (select student from seat where id = (i.id-1) )
when id%2 != 0 and id<(select count(student) from seat) then (select student from seat where id = (i.id+1) )
else student
end as student
from seat i
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助