LeetCode 626. Exchange Seats
考点 | 难度 |
---|---|
Database | Medium |
题目
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.
答案
SELECT
CASE
WHEN seat.id % 2 <> 0 AND seat.id = (SELECT COUNT(*) FROM seat) THEN seat.id
WHEN seat.id % 2 = 0 THEN seat.id - 1
ELSE
seat.id + 1
END as id,
student
FROM seat
ORDER BY id