代码拉取完成,页面将自动刷新
-- 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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。