Exchange Seats


  • 0

    Click here to see the full article post


  • 0

    select a.id, case
    when a.id%2 =1 then ifnull(b.student,a.student)
    else ifnull(c.student,a.student)
    end as student
    from seat a
    left join seat b on a.id = b.id-1
    left join seat c on a.id = c.id+1
    order by a.id asc


  • 0
    A

    select s1.id as id,ifnull(s2.student,s1.student) as student from seat s1 left join seat s2
    on s2.id-s1.id=if(s1.id%2=1,1,-1)
    order by id;


  • 0
    R

    select distinct
    s1.id
    , case when s2.student is null and mod(s1.id,2)=1 then s1.student
    when mod(s1.id,2)=1 then s2.student
    when mod(s1.id,2)=0 then s3.student
    end as student

    from seat as s1
    left join seat as s2 on s1.id=s2.id-1
    left join seat as s3 on s1.id=s3.id+1
    order by s1.id


  • 0
    T

    SELECT s.id,s.student FROM (
    SELECT id - 1 AS id, student FROM seat WHERE MOD(id, 2) = 0
    UNION
    SELECT id + 1 AS id, student FROM seat WHERE MOD(id, 2) = 1 AND id < (SELECT MAX(id) FROM seat)
    UNION
    SELECT id AS id, student FROM seat WHERE MOD(id, 2) = 1 AND id = (SELECT MAX(id) FROM seat)
    ) AS s ORDER BY id;


  • 0
    V

    SELECT seat.id, t.student
    FROM seat, seat t
    WHERE (seat.id != t.id AND CEIL(seat.id / 2) = CEIL(t.id / 2)) OR (seat.id = t.id AND seat.id = (SELECT MAX(u.id) FROM seat u) AND MOD(t.id, 2) = 1)
    ORDER BY id


  • 0
    S

    select (case
    when MOD(count(id),2) !=0
    case
    when mod(id,2)!=0 then id+1
    when mod(id,2)==0 then id-1
    when id = count(id) then id
    end
    end
    case
    when MOD(count(id),2) ==0
    case
    when mod(id,2)!=0 then id+1
    when mod(id,2)==0 then id-1
    end

        end)
        
        as id,student
        from seat
    

    Why does this not work?


  • 0
    X

    Write your MySQL query statement below

    select id , case mod(id , 2)
    when 0 then (select student from seat where id = out_seat.id - 1)
    when 1 then if( exists(select 1 from seat where id = out_seat.id + 1) ,
    (select student from seat where id = out_seat.id + 1) ,
    out_seat.student )
    end student
    from seat out_seat;


  • 0
    J

    select
    (case when mod(id,2)=0 then id-1
    when mod(id,2)=1 and id=( select count(*) from seat) then id
    else id+1 end) as id,
    student
    from seat
    order by id ASC;


Log in to reply
 

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