准备:

创建一个成绩表

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 分段执行_mySql 分段查询相关推荐

  1. mysql续型_mysql续集(查询部分)

    mysql> select goods_name,goods_id, concat("HTC",substring(goods_name,4)) as name from g ...

  2. 宝塔mysql慢日志_MySQL慢查询日志总结

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

  3. mysql 分段解析_MYSQL分段统计

    产品表 CREATE TABLE `product` ( `product_id` int(11) NOT NULL AUTO_INCREMENT, `product_model` varchar(2 ...

  4. mysql 缓存监控_MySql 缓存查询原理与缓存监控 和 索引监控

    MySql缓存查询原理与缓存监控 And 索引监控 by:授客 QQ:1033553122 查询缓存 1.查询缓存操作原理 mysql执行查询语句之前,把查询语句同查询缓存中的语句进行比较,且是按字节 ...

  5. mysql五大子句_MySQL的查询语句中可以使用以下哪个子句来表示分组查询

    [多选题]人类行为遗传学工作者倾向于把人的行为遗传分为哪几类 [填空题]MySQL的连接操作包括内连接.( )和交叉连接. [判断题]社会生活类尤其是人文风光类纪录片的解说则多用文学. 散文手法, 既 ...

  6. 查看mysql 更新命令_MySQL UPDATE 查询

    MySQL UPDATE 查询 如果我们需要修改或更新MySQL中的数据,我们可以使用 SQL UPDATE 命令来操作.. 语法 以下是 UPDATE 命令修改 MySQL 数据表数据的通用SQL语 ...

  7. MySQL获取连接_MySQL 连接查询超全详解

    1 作用 在MySQL中join操作被称为连接,作用是能连接多个表的数据(通过连接条件),从多个表中获取数据合并在一起作为结果集返回给客户端.例如: 表A: id name age 1 A 18 2 ...

  8. mysql报表慢_mysql慢查询日志报表工具mysqlsla

    一.安装 wget http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz tar xzvf mysqlsla-2.03.tar.gz cd mysqlsl ...

  9. mysql查找 提速_MySQL加速查询速度的独门武器:查询缓存

    [导读] 与朋友或同事谈到mysql查询缓存功能的时候,个人喜欢把Query Cache比作荔枝,是非常营养的东西,但是一次性吃太多了,就容易导致上火而流鼻血,虽然不是特别恰当的比喻,但是有很多相似的 ...

最新文章

  1. Spring中使用缓存时你应该知道的知识
  2. python使用random模块生成随机数、实现随机乱序和随机抽样?
  3. 【洛谷 1991】 无线通讯网
  4. c语言提取颜色,C语言颜色转换宏
  5. Python中str.replace()的使用方法
  6. MATLAB实战系列(十八)-遗传算法解决TSP(旅行商)问题-算法原理
  7. Java 调用 Caffe_解决 free(): invalid pointer: 0x00000000019ff700 运行时报错(caffe)(libtool使用)...
  8. python getopt参数参数自动补全_如何在Python中使用getopt / OPTARG?如果给出过多的参数(9),如何转移参数?...
  9. APC UPS 网络管理卡(型号apc ap9631)的配置
  10. win10家庭版 mysql_win10家庭版64位下mysql 8.0.15 安装配置方法图文教程
  11. ER图工具Visual Paradigm下载并设置中文
  12. Activiti6快速入门指南
  13. netmeeting的使用(详解)
  14. java开源bi_poli-java开源BI软件
  15. 【应用程序无法正常启动0xc000007b 请点击确定关闭应用程序】的错误如何解决?
  16. 三角函数回忆:三角恒等式、三角曲线、三角形的边角关系
  17. iOS14降级iOS13
  18. mysql占用内存过高_mysql数据库占用内存过高解决办法
  19. [note] 微电子学概论(二) PN结 MOS和MOSFET
  20. MySQLClient instal error: “raise Exception(”Wrong MySQL configuration: maybe https://bugs.mysql.com/

热门文章

  1. 【Docker】如何进入到Docker容器内部
  2. 阿里钉钉技术分享:企业级IM王者——钉钉在后端架构上的过人之处
  3. python中summary_Python summary_pb2.Summary方法代码示例
  4. 【第九章】vim程序编辑器
  5. 宁波市第一医院附近的房屋调研
  6. 移动机器人传感器——GNSS
  7. word中使用mathtype编辑公式并添加序号
  8. (转)Winton:如何在100多个期货市场交易
  9. 后台Base64解码图片变小的坑
  10. sonarqube如何导入规则_sonar如何添加自定义JAVA规则