【leetcode】626. 换座位

题目:

小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。

其中纵列的 id 是连续递增的

小美想改变相邻俩学生的座位。

你能不能帮她写一个 SQL query 来输出小美想要的结果呢?

答案:

# Write your MySQL query statement below
select s.id , s.student from
(
select id-1 as id , student from seat where mod(id,2)=0
union
select id+1 as id , student from seat where mod(id,2) = 1 and id != (select count(*) from seat)
union
select id , student from seat where mod(id,2) = 1 and id = (select count(*) from seat)
) s order by id;


你可能感兴趣的:(leetcode学习记录)