准备:

创建一个成绩表

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. VirtualBox 复制vdi文件和修改vdi的uuid

    1.复制vdi文件:VBoxManage clonehd 因为VirtualBox不允许注册重复的uuid,而每个vdi文件都有一个唯一的uuid.所以要想拷贝一份vdi文件再次在VBOX中注册,简单 ...

  2. WiresShark 使用方法

    Wireshark(前称Ethereal)是一款功能强大的网络抓包分析工具,在我的工作中是不可或缺的一部分工具,往往在网络出现异常时,查看网络中的数据包,会豁然开朗.1.菜单栏  主菜单包括以下几个项 ...

  3. FLASH和EEPROM的最大区别

    源:http://www.cnblogs.com/bingoo/p/3551753.html FLASH和EEPROM的最大区别是FLASH按扇区操作,EEPROM则按字节操作,二者寻址方法不同,存储 ...

  4. C#入门经典-第15章ListBox,CheckedListBox

  5. byte[]和InputStream的相互转换

    1:byte[]转换为InputStream InputStream sbs = new ByteArrayInputStream(byte[] buf); 2:InputStream转换为Input ...

  6. src和href的区别

    <html><!--头部标签--><!--src和href的区别src:引用,该资源是页面不可缺少的一部分,如(img标签 video标签 radio标签);href:引 ...

  7. 使用traceview进行Android性能测试(转)

    使用traceview进行Android性能测试(转) 使用traceview进行Android性能测试  原文链接:http://www.cnblogs.com/Android-and-androi ...

  8. SqlServer批量导入

    SQL Server的Bulk Insert语句可以将本地或远程的数据文件批量导入到数据库中,速度非常的快.远程文件必须共享才行,文件路径须使用通用约定(UNC)名称,即"\\服务器名或IP ...

  9. java中单例设计模式

    在java中创建单例的方式主要有三种:饿汉式.懒汉式.登记式.以下内容均是摘抄自 http://blog.csdn.net/jason0539/article/details/23297037/ 一. ...

  10. java fx example

    http://www.java2s.com/Tutorials/Java/JavaFX/1500__JavaFX_WebEngine.htm