if(id < (select count(*) from seat), if(id mod 2=0, id-1, id+1), if(id mod 2=0, id-1, id)) as id, student
order by id asc;
use 2 if() instead of 3:
select if (
if( id<> (select max(id) from seat), id+1, id),
) id, student from seat order by id;
SELECT CASE WHEN seat.id % 2 <> 0 AND seat.id = (SELECT COUNT(*) FROM seat) THEN seat.id WHEN seat.id % 2 = 0 THEN seat.id - 1 ELSE seat.id + 1 END as id, student FROM seat ORDER BY id ;
Does anyone know how to use IF statement in TSQL? I have to use CASE here. Thank you.
select if(id < (select count(*) from seat), id + 2 * mod(id,2) - 1, id+mod(id,2)-1) as id, student from seat order by id
Although @ykyk's solution looks a little better depending on how the compiler executes it.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.