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;


  • 0
    1

    SELECT
    (CASE
    WHEN MOD(id, 2) != 0 AND id!= (select count() from seat) THEN id + 1
    WHEN MOD(id, 2) != 0 AND id =(select count(
    ) from seat) THEN id
    ELSE id-1
    END) AS id, student FROM seat
    ORDER BY id ASC;


  • 0
    Y

    Select A.newid as id, A.student
    From(
    Select
    CASE WHEN (Select max(id) from seat)%2=0 Then
    Case WHEN id%2=0 Then id-1
    Else id+1 End
    Else Case When id%2=0 Then id-1
    When id=(Select max(id) from seat) Then id
    Else id+1 End End AS newid
    ,student
    From seat
    ) as A
    order by A.newid


  • 0
    S

    Write your MySQL query statement below

    select * from (select s1.id id, s2.student student from seat s1 join seat s2 on (s2.id - s1.id) = 1
    where mod(s1.id,2) = 1
    union
    select s1.id id,s2.student student from seat s1 join seat s2 on (s1.id - s2.id) = 1
    where mod(s1.id,2) = 0
    union
    select id, student from seat where id = (select max(id) from seat) and mod(id,2) = 1) newTable
    order by id


  • 0

    SELECT id,
    student,
    DECODE (idx,1, NVL(ld,student), 0,NVL(lg,student),NVL(ld,lg))
    FROM
    (SELECT id ,
    student,
    lag(student,1) over (order by id) AS lg,
    lead(student,1) over (order by id) AS ld,
    mod(id,2) idx
    FROM seat
    );


Log in to reply
 

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