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开始的.下面总结一些使用过或者研究过的经验,从配置以 ...
随机推荐
- Tomcat的class加载的优先顺序
Tomcat的class加载的优先顺序一览 1.最先是$JAVA_HOME/jre/lib/ext/下的jar文件. 2.环境变量CLASSPATH中的jar和class文件. 3.$CATALINA ...
- HTML DOCTYPE文档类型举例说明
HTML DOCTYPE文档类型举例说明 HTML4.01文档过渡定义类型,此类型定义的文档可以使用HTML中的标签与元素包括一些不被W3C推荐的标签(例如:font.b等),不可以使用框架 < ...
- access restriction
一.既然存在访问规则,那么修改访问规则即可.打开项目的Build Path Configuration页面,打开报错的JAR包,选中Access rules条目,选择右侧的编辑按钮,添加一个访问规则即 ...
- java集合框架工具类Collections,集合的操作
1 import java.util.*; public class asList { public static void main(String args[]) { // int arr[] = ...
- 页面加载完毕执行多个JS函数
通常我们需要在打开页面时加载脚本,这些脚本必须在页面加载完毕后才可以执行,因为这时候DOM才完整,可以利用window.onload确保这一点,如:window.onload=firstFunctio ...
- FusionCharts使用问题及解决方法(二)-FusionCharts常见问题大全
在上文中,我们介绍了FusionCharts常见问题(FAQ)的解决方法,本文将一同讨论FusionCharts使用者面临的一些复杂问题的解决方法. 如何启用JavaScript调试模式? 要启用Ja ...
- 穿越泥地(mud)
穿越泥地(mud) 题目描述 清早6:00,FJ就离开了他的屋子,开始了他的例行工作:为贝茜挤奶.前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想象,FJ现在面对的 是一大片泥泞的土地.FJ ...
- div使用
div style常用属性 一.常用属性: 1.Height:设置DIV的高度. 2.Width:设置DIV的宽度. 例: <div style="width:200px;height ...
- js学习之函数
1/.js中函数就是对象. 2/以表达式方式定义的函数一般不要函数名以使代码紧凑. 3/js中函数声明的方式会被默认提到外部脚本或最前面,所以在其定义前的代码也可调用. 然而以表达式方式的则不行. 4 ...
- 请教<context:component-scan/>和<mvc:annotation-driven/>的区别20
http://www.iteye.com/problems/66133 FileSystemXmlApplicationContext