1 题目
2 MySQL
2.1 方法一(case when)
注意是case when,不是 case 列
select (case
when mod(id, 2) != 0 and id = counts then id # 奇数,且是最后一行
when mod(id, 2) != 0 and id != counts then id + 1 # 奇数,非最后一行
else id - 1 # 偶数
end) as id, student
from seat, (select count(*) as counts from seat) as B # B表仅为得到记录总数
order by id asc;
加速
select (case
when id & 1 = 1 and id = counts then id # 奇数,且是最后一行
when id & 1 = 1 and id <> counts then id + 1 # 奇数,非最后一行
else id - 1 # 偶数
end) as id, student
from seat, (select count(*) as counts from seat) as B # B表仅为得到记录总数
order by id asc;