★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10472774.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
1 Create table If Not Exists seat(id int, student varchar(255)) 2 Truncate table seat 3 insert into seat (id, student) values ('1', 'Abbot') 4 insert into seat (id, student) values ('2', 'Doris') 5 insert into seat (id, student) values ('3', 'Emerson') 6 insert into seat (id, student) values ('4', 'Green') 7 insert into seat (id, student) values ('5', 'Jeames')
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.
小美是一所中学的信息科技老师,她有一张 seat
座位表,平时用来储存学生名字和与他们相对应的座位 id。
其中纵列的 id 是连续递增的
小美想改变相邻俩学生的座位。
你能不能帮她写一个 SQL query 来输出小美想要的结果呢?
示例:
+---------+---------+ | id | student | +---------+---------+ | 1 | Abbot | | 2 | Doris | | 3 | Emerson | | 4 | Green | | 5 | Jeames | +---------+---------+
假如数据输入的是上表,则输出结果如下:
+---------+---------+ | id | student | +---------+---------+ | 1 | Doris | | 2 | Abbot | | 3 | Green | | 4 | Emerson | | 5 | Jeames | +---------+---------+
注意:
如果学生人数是奇数,则不需要改变最后一个同学的座位。
Runtime: 152 ms
1 # Write your MySQL query statement below 2 select 3 (case when id % 2 = 0 then id - 1 4 when id % 2 = 1 and id <> c.cnt then id + 1 5 else id 6 end) as id, student 7 from 8 seat, 9 (select count(id) as cnt from seat) as c 10 order by id
253ms
1 # Write your MySQL query statement below 2 SELECT 3 (CASE 4 WHEN MOD(id, 2) != 0 AND counts != id THEN id + 1 5 WHEN MOD(id, 2) != 0 AND counts = id THEN id 6 ELSE id - 1 7 END) as id, 8 student 9 FROM 10 seat, 11 (SELECT COUNT(*) as counts 12 FROM seat) as seat_counts 13 ORDER BY id ASC;
255ms
1 # Write your MySQL query statement below 2 select * from ( 3 select case when b.Id is null then a.Id else b.Id end as Id,a.student from seat a left join seat b on a.Id+1=b.Id where mod(a.id,2)=1 4 union 5 select b.Id,a.student from seat a left join seat b on a.Id-1=b.Id where mod(a.id,2)=0 6 ) a order by a.id
257ms
1 select 2 case 3 when id%2=1 and id=(select max(id) from seat) then id 4 when id%2=1 then id+1 5 else id-1 end as id, 6 student 7 FROM seat 8 order by id
258ms
1 # Write your MySQL query statement below 2 select case 3 when mod(id,2) =1 and id = (select max(id) as sid from seat) then id 4 when mod(id,2) =1 then id+1 5 else id-1 end as id, student 6 from seat 7 order by id
260ms
1 SELECT 2 (CASE WHEN id%2=0 THEN id-1 3 WHEN id%2=1 AND id<(select max(id) FROM seat) THEN id+1 4 ELSE id 5 END) as id 6 ,student 7 FROM seat 8 ORDER BY id;