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

  • 相关阅读:
    COGNOS10启动服务报错 问题解决
    Linux 下 新增Oracle10g 实例 (转自http://www.cnblogs.com/lan0725/archive/2011/07/18/2109474.html)
    WIN7安装COGNOS8后配置IIS网站后,访问COGNOS站点网页一直显示空白,解决方法(转载)
    Gridview光棒效果 鼠标滑过
    11款实用的一句话网站设计代码
    自定义js方法 (格式化时间)
    测试一下
    UTF7转换GB2312编码的方法(中文)
    HTML注册页面验证注册信息
    android欢迎页
  • 原文地址:https://www.cnblogs.com/Springmoon-venn/p/5510526.html
Copyright © 2011-2022 走看看