mySql 分段查询

准备:

创建一个成绩表

                Create table grade (id integer, score integer);

插入数据(只有id每次加一,score是1到100的随机数,java生成):

               

public class GradeInsertSentence {

 

    public static void main(String[] args) {

 

         for (int i = 0; i < 100; i++) {

             int j = (int) (Math.random()*100) + 1;

             System.out.println("insert into grade(id,score) value('"+i+"','"+j+"');");

         }

    }

 

}

 

查询grade表的所有数据

                Select * from grade;

需求:

                查询指定分段的人数(x>=80; 80>x>=60; 60>x>40; 40>x>=20, x<20 )

Sql:

  

实现1:

select *

from

                (select count(*) as A from grade g where g.score >=80) a,

                (select count(*) as B from grade g where g.score >=60 and g.score <80) b,

                (select count(*) as C from grade g where g.score >=40 and g.score <60) c,

                (select count(*) as D from grade g where g.score >=20 and g.score <40) d,

                (select count(*) as E from grade g where g.score <20) e;

或者:

select a.aa, b.bb, c.cc, d.dd, e.ee

from

                (select count(*) as aa from grade g where g.score >=80) a,

                (select count(*) as bb from grade g where g.score >=60 and g.score <80) b,

                (select count(*) as cc from grade g where g.score >=40 and g.score <60) c,

                (select count(*) as dd from grade g where g.score >=20 and g.score <40) d,

                (select count(*) as ee from grade g where g.score <20) e;

 

实现2:

select count(*) as aa from grade g where g.score >=80

union all

select count(*) as bb from grade g where g.score >=60 and g.score <80

union all

select count(*) as cc from grade g where g.score >=40 and g.score <60

union all

select count(*) as dd from grade g where g.score >=20 and g.score <40

union all

select count(*) as ee from grade g where g.score <20

 

这个比较尴尬的是显示出来的结果是这样的:

 

还有就是,如果其中一个分段的是没有值得,那就只会显示4条结果,最重要的是,你还不知道是哪一个分段没有结果。。。。。

实现3:

select

                case when (score >=80) then 'A'

                when (score >=60 and score <80) then 'B'

                when (score >=40 and score <60) then 'C'

                when (score >=20 and score <40) then 'D'

                else 'E'

                end grade, count(*) num

from grade group by

                case when (score >=80) then 'A'

                when (score >=60 and score <80) then 'B'

                when (score >=40 and score <60) then 'C'

                when (score >=20 and score <40) then 'D'

                else 'E' end

                order by 1;

select

                case when (score >=80) then 'A'

                when (score >=60 and score <80) then 'B'

                when (score >=40 and score <60) then 'C'

                when (score >=20 and score <40) then 'D'

                else 'E'

                end 'grade', count(*) num

from grade

group by

                case when (score >=80) then 'A'

                when (score >=60 and score <80) then 'B'

                when (score >=40 and score <60) then 'C'

                when (score >=20 and score <40) then 'D'

                else 'E' end;

实现4:

select A.score*20, count(A.score) from

(

                select floor(g.score/20) as score from grade g

)  A

group by A.score;

或(有错,不会用convert

select convert(A.score*20,varchar) ,count(A.score)   from

(

                select floor(g.score/20) as score from grade g

) A

group by A.score;

 

 

实现5:(错的)

               

select

                case when score BETWEEN 80 AND 100 then 'A'

                when score BETWEEN 60 AND 80 then 'B'

                when score BETWEEN 40 AND 60 then 'C'

                when score BETWEEN 20 AND 40 then 'D'

                when score < 20 then 'E' end as 'grade',

count(*) as 'num' FROM grade;

 都是在百度上找的,最后一个实现不成功,between and在select里面不能识别范围,哪位仁兄看到,实现了,记得给我留言,谢谢。

转载于:https://www.cnblogs.com/Springmoon-venn/p/5510526.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值