准备:

创建一个成绩表

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

mySql 分段查询的更多相关文章

  1. 企业级中带你ELK如何实时收集分析Mysql慢查询日志

    什么是Mysql慢查询日志? 当SQL语句执行时间超过设定的阈值时,便于记录到指定的日志文件中或者表中,所有记录称之为慢查询日志 为什么要收集Mysql慢查询日志? 数据库在运行期间,可能会存在这很多 ...

  2. MySQL将查询出来的一组数据拼装成一个字符串

    1 前言 由于项目中有一个需求,需要把当日当周的排行榜数据归档,以便后期查询和发放奖励.然而发现,mysql的变量只能存一个变量值,然后如果要储存一条记录,可以使用CONCAT_WS,如果要储存多条记 ...

  3. Linux下MySQL慢查询分析mysqlsla安装使用

    说明: 操作系统:CentOS 5.X 64位 MySQL版本:mysql-5.5.35 MySQL配置文件:/etc/my.cnf MySQL 数据库存放目录:/data/mysql 实现目的:开启 ...

  4. MySQL的查询计划中ken_len的值计算

    本文首先介绍了MySQL的查询计划中ken_len的含义:然后介绍了key_len的计算方法:最后通过一个伪造的例子,来说明如何通过key_len来查看联合索引有多少列被使用. key_len的含义 ...

  5. mysql的查询、子查询及连接查询

    >>>>>>>>>> 一.mysql查询的五种子句         where(条件查询).having(筛选).group by(分组). ...

  6. MySQL慢查询日志总结

    慢查询日志概念 MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志 ...

  7. 【转】Mysql联合查询union和union all的使用介绍

    Mysql的联合查询命令UNION和UNION ALL,总结了使用语法和注意事项,以及学习例子和项目例子,需要的朋友可以参考下 一.UNION和UNION ALL的作用和语法 UNION 用于合... ...

  8. mysql慢查询日志分析工具 mysqlsla(转)

    mysql数据库的慢查询日志是非常重要的一项调优辅助日志,但是mysql默认记录的日志格式阅读时不够友好,这是由mysql日志记录规则所决定的,捕获一条就记录一条,虽说记录的信息足够详尽,但如果将浏览 ...

  9. Mysql慢查询和慢查询日志分析

     Mysql慢查询和慢查询日志分析   众所周知,大访问量的情况下,可添加节点或改变架构可有效的缓解数据库压力,不过一切的原点,都是从单台mysql开始的.下面总结一些使用过或者研究过的经验,从配置以 ...

随机推荐

  1. Windows修改hosts文件及位置

    文件位置 C:\Windows\System32\drivers\etc 中的hosts文件 修改方式 指定IP 域名 192.168.66.7 www.baidu.com

  2. docker安装hadoop

    docker为hadoop的云化带来了极大便利,安装和应用也会更快更方便.进入正题: docker search hadoop 将会看到如下结果: INDEX NAME DESCRIPTION STA ...

  3. u-boot添加一个hello命令

    1.在common目录下建立一个cmd_hello.c文件 2.仿照/common/cmd_bootm.c文件修改,把cmd_bootm.c头文件复制过来 3.再复制do_bootm.U_BOOT_C ...

  4. 配置glance使用NFS后端

    首先先使用“glance image-delete”命令删除所有镜像,释放磁盘空间. 停止glance服务:service openstack-glance-api stopservice opens ...

  5. c#动态生成word,在本地可以执行,但发布到iis上出错解决方案

    报错点: Microsoft.Office.Interop.Word.DocumentClass.SaveAs 解决方案: 1.在"开始"->"运行"中输 ...

  6. 皓轩的jquery mobile之路(二)

    jQuery Mobile 使用 HTML5 & CSS3 最小的脚本来布局网页. 编写代码要注意最外层div需要添加data-role="page" ,标题需要添加dat ...

  7. php使用curl设置超时的重要性

    原文:http://phpquan.com/lamp/php/php-curl-timeout/ 网站登录不了,原因是没有可用的 PHP 子进程来响应新的请求了.这可能是是由于PHP-curl  没有 ...

  8. javascript 基础系列(二)

    原文参考:http://www.cnblogs.com/libin-1/p/5955208.html 下图是用Illustrator制作的可视化信息图,希望能帮你理清Javascript对象与__pr ...

  9. js中判断输入框是否为空(判断是一串空的字符串)

    function ltrim(str) {  if(str.length==0)    return(str);   else {    var idx=0;    while(str.charAt( ...

  10. h2database. 官方文档

    http://www.h2database.com/html/advanced.html http://www.h2database.com/html/tutorial.html#csv http:/ ...