#112. [LeetCode / MySQL] 626. Exchange Seats

문제

 

 

입력 코드

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