mysql有意思的题目记录(持续更新)

 1.写sql,查找一张表中分数在0-60,60-80,80-100三个层次的学生人数

  • 用到了if函数:if(条件,a,b)
    • 条件为true,返回a,否则返回b
select sum(if(score between 0 and 60,1,0)) as '0-60',sum(if(score between 60 and 80,1,0)) as '60-80',
sum(if(score BETWEEN 80 and 100,1,0)) as '80-100' from myscores
-- 可以替换下列的三条sql,节省系统的开销
select count(*) from myscores where score between 0 and 60;
select count(*) from myscores where score between 60 and 80;
select count(*) from myscores where score between 80 and 100;

猜你喜欢

转载自blog.csdn.net/future_god_qr/article/details/121509013