mysql分段数据汇总

27 篇文章 0 订阅
CREATE TABLE `ljbb`.`Untitled`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `class` int(10) NOT NULL,
  `score` int(3) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `score` (`id`, `name`, `class`, `score`) VALUES
(1, '张三', 1, '60'),
(2, '李四', 2, '80'),
(3, '张三1', 1, '20'),
(4, '李四2', 2, '40'),
(5, '张三2', 1, '50'),
(6, '李四3', 2, '90'),
(7, '王五', 1, '90'),
(8, '李六', 2, '85');
SELECT  
 sum(CASE when score<60 then 1 else 0 end) AS '不及格',  
 sum(CASE when score>=60 and score<70 then 1 else 0 end) AS '差',  
 sum(CASE when score>=70 and score<80 then 1 else 0 end) AS '中',  
 sum(CASE when score>=80 and score<90 then 1 else 0 end) AS '良',  
 sum(CASE when score>=90 and score<=100 then 1 else 0 end) AS '优'  
from score;

如果是每20分段为一组可以这样查询

select count(*),(ceil(score/20)-1)*20 as min,ceil(score/20)*20 as max from score group by ceil(score/20)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值