zoukankan      html  css  js  c++  java
  • 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里面不能识别范围,哪位仁兄看到,实现了,记得给我留言,谢谢。

  • 相关阅读:
    Linux系统服务
    Linux进程管理
    Linux压缩打包
    Linux输入输出
    Linux权限管理
    Linux用户管理
    Linux文件管理
    Linux-Shell
    Centos7 安装jdk1.8
    Python数据分析之路
  • 原文地址:https://www.cnblogs.com/Springmoon-venn/p/5510526.html
Copyright © 2011-2022 走看看