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里面不能识别范围,哪位仁兄看到,实现了,记得给我留言,谢谢。
mySql 分段查询的更多相关文章
- 企业级中带你ELK如何实时收集分析Mysql慢查询日志
什么是Mysql慢查询日志? 当SQL语句执行时间超过设定的阈值时,便于记录到指定的日志文件中或者表中,所有记录称之为慢查询日志 为什么要收集Mysql慢查询日志? 数据库在运行期间,可能会存在这很多 ...
- MySQL将查询出来的一组数据拼装成一个字符串
1 前言 由于项目中有一个需求,需要把当日当周的排行榜数据归档,以便后期查询和发放奖励.然而发现,mysql的变量只能存一个变量值,然后如果要储存一条记录,可以使用CONCAT_WS,如果要储存多条记 ...
- Linux下MySQL慢查询分析mysqlsla安装使用
说明: 操作系统:CentOS 5.X 64位 MySQL版本:mysql-5.5.35 MySQL配置文件:/etc/my.cnf MySQL 数据库存放目录:/data/mysql 实现目的:开启 ...
- MySQL的查询计划中ken_len的值计算
本文首先介绍了MySQL的查询计划中ken_len的含义:然后介绍了key_len的计算方法:最后通过一个伪造的例子,来说明如何通过key_len来查看联合索引有多少列被使用. key_len的含义 ...
- mysql的查询、子查询及连接查询
>>>>>>>>>> 一.mysql查询的五种子句 where(条件查询).having(筛选).group by(分组). ...
- MySQL慢查询日志总结
慢查询日志概念 MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志 ...
- 【转】Mysql联合查询union和union all的使用介绍
Mysql的联合查询命令UNION和UNION ALL,总结了使用语法和注意事项,以及学习例子和项目例子,需要的朋友可以参考下 一.UNION和UNION ALL的作用和语法 UNION 用于合... ...
- mysql慢查询日志分析工具 mysqlsla(转)
mysql数据库的慢查询日志是非常重要的一项调优辅助日志,但是mysql默认记录的日志格式阅读时不够友好,这是由mysql日志记录规则所决定的,捕获一条就记录一条,虽说记录的信息足够详尽,但如果将浏览 ...
- Mysql慢查询和慢查询日志分析
Mysql慢查询和慢查询日志分析 众所周知,大访问量的情况下,可添加节点或改变架构可有效的缓解数据库压力,不过一切的原点,都是从单台mysql开始的.下面总结一些使用过或者研究过的经验,从配置以 ...
随机推荐
- discuz!迁移指南
转自:http://jingyan.baidu.com/article/f7ff0bfc77114b2e26bb1390.html 曾经在本地搭建过一个discuz!论坛,现在买了域名和服务器,那么怎 ...
- 修改document.domain的注意事项(转)
有时候,需要修改document.domain. 典型的情形:http://a.xxx.com/A.htm 的主页面有一个<iframe src="http://b.xxx.com/B ...
- telnet关闭tomcat
telnet localhost 8005然后输入SHUTDOWN即可关闭tomcat 前提是8005端口已打开
- 基于Spring的异步系统实现方案
一般的实现方案 发送异步消息所使用的工具类: import java.util.Date; import javax.jms.Destination; import javax.jms.JMSExce ...
- service引用
已引用 attachmentManager 为例 ApplicationContext applicationContext = WebApplicationContextUtils.getWebAp ...
- 给ThinkPHP5增加验证码功能
就在这几天,TP5进行的RC3的大规模更新,虽然我们都狠狠地骂了一百遍,但是我的内心是无比的激动,TP终于走上了"上流社会"的模式:composer! 为什么说composer是上 ...
- ICE异步程序设计-----AMI/AMD
1 简介 AMI 异步方法调用(AMI) 这个术语描述的是客户端的异步编程模型支持. 如果你使用AMI 发出远地调用,在Ice run time 等待答复的同时,发出调用的线程不会阻塞.相反,发出调用 ...
- Android启动脚本init.rc(2)
在Android中使用启动脚本init.rc,可以在系统的初始化中进行简单的操作. init.rc启动脚本路径:system/core/rootdir/init.rc 内容: Commands:命令 ...
- javascript 基础 onclick(this)用法介绍
http://www.5idev.com/p-javascript_events_onclick.shtml --------------------------------------------- ...
- DML、DDL、DCL区别及介绍
DML(data manipulation language): 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作 ...