straightforward solution using 3 joins


  • 1
    P

    nothing fancy but very straightforward, just update the student name by the definition of "swap"

    select s1.id,
    case when mod(s1.id, 2)=1 then 
        case when s2.student is null then s1.student else s2.student end
        else s3.student end as student
    from seat s1
    left join seat s2
    on s1.id=s2.id-1
    left join seat s3
    on s1.id=s3.id+1
    order by s1.id asc
    

  • 0

    almost the same with yours:

    SELECT s.id, IF(MOD(s.id, 2) = 1, IFNULL(n.student, s.student), p.student) AS student
    FROM seat s
    LEFT JOIN seat p ON s.id - 1 = p.id
    LEFT JOIN seat n ON s.id + 1 = n.id
    ORDER BY id;
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.