-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path626-ExchangeSeats.sql
More file actions
105 lines (102 loc) · 2.57 KB
/
626-ExchangeSeats.sql
File metadata and controls
105 lines (102 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
-- 626. Exchange Seats
-- Table: Seat
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | id | int |
-- | name | varchar |
-- +-------------+---------+
-- id is the primary key column for this table.
-- Each row of this table indicates the name and the ID of a student.
-- id is a continuous increment.
--
-- Write an SQL query to swap the seat id of every two consecutive students.
-- If the number of students is odd, the id of the last student is not swapped.
-- Return the result table ordered by id in ascending order.
-- The query result format is in the following example.
--
-- Input:
-- Seat table:
-- +----+---------+
-- | id | student |
-- +----+---------+
-- | 1 | Abbot |
-- | 2 | Doris |
-- | 3 | Emerson |
-- | 4 | Green |
-- | 5 | Jeames |
-- +----+---------+
-- Output:
-- +----+---------+
-- | id | student |
-- +----+---------+
-- | 1 | Doris |
-- | 2 | Abbot |
-- | 3 | Green |
-- | 4 | Emerson |
-- | 5 | Jeames |
-- +----+---------+
-- Explanation:
-- Note that if the number of students is odd, there is no need to change the last one's seat.
-- Create table If Not Exists Seat (id int, student varchar(255))
-- Truncate table Seat
-- insert into Seat (id, student) values ('1', 'Abbot')
-- insert into Seat (id, student) values ('2', 'Doris')
-- insert into Seat (id, student) values ('3', 'Emerson')
-- insert into Seat (id, student) values ('4', 'Green')
-- insert into Seat (id, student) values ('5', 'Jeames')
-- Write your MySQL query statement below
-- CASE WHEN
SELECT
(
CASE
WHEN MOD(id,2) = 1 AND id = (SELECT COUNT(*) FROM seat) THEN id -- 如果是奇数最后一个同学保持不变
WHEN MOD(id,2) = 1 THEN id + 1 -- 奇数向后
ElSE id - 1 -- 偶数向前
END
) AS id,
student
FROM
seat
ORDER BY
id;
-- UNION
-- UNION
SELECT
*
FROM
(
(-- 不是最后一个的奇数 +1 变偶数
SELECT
id+1 AS id, student
FROM
seat
WHERE
id % 2 = 1 AND
id NOT IN ( SELECT MAX(id) FROM seat )
)
UNION
( -- 编号偶数 - 1 变奇数
SELECT
id - 1 AS id, student
FROM
seat
WHERE
id % 2 = 0
)
UNION
(-- 最后一个奇数不变
SELECT
id AS id,
student
FROM
seat
WHERE
id % 2 = 1 AND
id IN (SELECT MAX(id) FROM seat)
ORDER BY
id
)
) AS a
ORDER BY
id ASC