문제
입력 코드
1.
SELECT
CASE
WHEN id = (SELECT MAX(id) FROM Seat) AND (id % 2 = 1)
THEN id
WHEN id % 2 = 1
THEN id + 1
ELSE id - 1
END AS id, student
FROM Seat
ORDER BY id;
2.
SELECT S.id,
IFNULL(
IF(S.id % 2 = 0,
(SELECT student FROM Seat WHERE id = S.id - 1),
(SELECT student FROM Seat WHERE id = S.id + 1)
),
S.student
) AS student
FROM Seat S
ORDER BY S.id;
코드 설명
#SELECT #CASE #WHEN #MAX #AS #ORDER BY #IFNULL #IF #WHERE #AND
문제 출처
https://leetcode.com/problems/exchange-seats/description/?envType=study-plan-v2&envId=top-sql-50
'SQL' 카테고리의 다른 글
#114. [LeetCode / MySQL] 1321. Restaurant Growth (0) | 2024.06.18 |
---|---|
#113. [LeetCode / MySQL] 1341. Movie Rating (0) | 2024.06.18 |
#111. [LeetCode / MySQL] 1978. Employees Whose Manager Left the Company (0) | 2024.06.17 |
#110. [LeetCode / MySQL] 1907. Count Salary Categories (0) | 2023.10.06 |
#109. [LeetCode / MySQL] 1204. Last Person to Fit in the Bus (0) | 2023.10.05 |