准备:

创建一个成绩表

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. 转 json数组对象和对象数组

    一.Json的简单介绍 从结构上看,所有的数据最终都可以分成三种类型: 第一种类型是scalar(标量),也就是一个单独的string(字符串)或数字(numbers),比如“北京”这个单独的词. 第 ...

  2. jsp如果超过字数就变成...

    <script> var infoTitle = '<ww:property value="infoTitle"/>'; if(infoTitle.leng ...

  3. zf-关于把某个地址的svn项目移动到另一个上面的步骤

    1 首先查检出来 2 然后断开连接 删除元信息(这个必须注意,很敏感的,不然酒吧元信息带入到了另外一个svn地址中了) 3 之后再到另外一个svn上建立远程资源文件,把项目导入进去,注意不要导入错了, ...

  4. 用JavaScript 来将数字转换成字符。

    背景: 一切嵌入式设备上面的信息,比如设备名称,设备时区是可以写入到设备上面的寄存器中的(一个寄存器两个字节,2*8 bit),比如 -1 ,写入到寄存器中为 2d31,然后可以通过一些进程将寄存器中 ...

  5. automaticallyAdjustsScrollViewInsets 标签栏不正常显示

    想做的效果如下: 结果那个首页.手办模型神马的就是不显示啊... 这个标签栏是用scrollView做的,解决办法: viewController.automaticallyAdjustsScroll ...

  6. ARM系统中函数调用过程中的参数传递-转

    在 嵌入式软件编程中,经常会用到函数调用,之前在学习如何在C语言中嵌入汇编时有了解到C语言之前的参数调用是使用寄存器R0传递第一个参数,R1传递到第 二个..一直到R3传递第四个参数.但是实际上有时可 ...

  7. VMware NAT端口映射外网访问虚拟机linux

    本文目的: 一. SSH连接 二. 访问HTTP VMware Workstation提供了两种虚拟机上网方式,一种bridge,一种NAT,bridge可以获得公网地址,而NAT只能是内网地址了. ...

  8. 进程间通信——FIFO(多个客户进程,一个服务进程)

    FIFO简介 FIFO就是Unix的一种复合POSIX标准的进程间通信机制.他又称为命名管道,跟管道的不同点是,每个FIFO都有一个路径名与之关联. FIFO虽然有路径名,但是他这中文件是在内核态(管 ...

  9. (Question)CSS中position的绝对定位问题

    RT,绝对定位相对于定位的元素存在是哪里? https://yunpan.cn/crjSMTiak2srZ  访问密码 1570

  10. BootStrap中关于input-group的问题(未解决)

    ****************************************2016年1月6日 23:08******************************* 本来想实现的如下功能: 但 ...