1.第一遍写的时候整太麻烦了,先是子查询取了job和count,然后再在子查询的基础上通过if判断奇偶数并分别求中位数,并用cast把小数转成整数。这里还踩了个大坑,cast没有int这个datatype,这里int是signed或者unsigned。太麻烦太麻烦了
select temp.job,
if(temp.cnt % 2 <> 0, CAST((temp.cnt+1)/2 as UNSIGNED), CAST(temp.cnt/2 as UNSIGNED)) as start,
if(temp.cnt % 2 <> 0, CAST((temp.cnt+1)/2 as UNSIGNED), CAST(temp.cnt/2+1 as UNSIGNED)) as end
from (select job, count(id) as cnt from grade
group by job) as temp
order by temp.job
2.这也太简单了!!!ceil=ceiling是向上取整,floor=flooring是向下取整
select job, floor((count(id)+1)/2), ceil((count(id)+1)/2) from grade
group by job
order by job