[SQL]LeetCode626. 換座位 | Exchange Seats

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-zsvcvevx-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

SQL架構git

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.github

The column id is continuous increment. sql

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: spa

+---------+---------+
|    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.code


小美是一所中學的信息科技老師,她有一張 seat 座位表,平時用來儲存學生名字和與他們相對應的座位 id。htm

其中縱列的 id 是連續遞增的blog

小美想改變相鄰倆學生的座位。

你能不能幫她寫一個 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;
相關文章
相關標籤/搜索