Note: If the number of students is odd, there is no need to change the last one’s seat.
case语句
1 2 3 4 5 6 7
# Write your MySQL query statement below select t.id, ( select student from seat as t1 where t1.id = (casewhen t.id % 2 = 0then t.id - 1 when t.id % 2 = 1and t.id < (selectcount(*) from seat) then t.id + 1 else t.id end) ) as student from seat as t;
窗口函数
1 2 3 4 5 6 7
# Write your MySQL query statement below select t.id, (casewhenid % 2 = 0then lastStudent else nextStudent end) as student from (selectid, student, lag(student, 1, student) over(orderbyid) as lastStudent, lead(student, 1, student) over(orderbyid) as nextStudent from seat) as t;