mysql 范围统计(-1)

-- 创建数据表
create table score(id int not null auto_increment,score int not null,primary key (id))engine=myisam;

-- 插入数据
insert into score(`score`) values(100),(200),(200),(100),(300),(400),(500),(300),(200),(400),(500),(200),(500),(300);

-- 范围分组


SELECT 
count(a.score_class),a.score_class
FROM
    (SELECT 
        id,
            score,
            (CASE
                WHEN score >= 100 AND score <= 200 THEN 1
                WHEN score > 200 AND score <= 300 THEN 2
                WHEN score > 300 AND score <= 400 THEN 3
                WHEN score > 400 AND score <= 500 THEN 4
                ELSE 5
            END) AS score_class
    FROM
        score) AS a   group by a.score_class;

-- output 

+----------------------+-------------+
| count(a.score_class) | score_class |
+----------------------+-------------+
|                    7 |           1 |
|                    3 |           2 |
|                    2 |           3 |
|                    3 |           4 |
+----------------------+-------------+
4 rows in set (0.00 sec)

-- index
mysql> show index from score;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| score |          0 | PRIMARY  |            1 | id          | A         |          15 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

-- explain

+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
|  1 | PRIMARY     | <derived2> | ALL  | NULL          | NULL | NULL    | NULL |   15 | Using temporary; Using filesort |
|  2 | DERIVED     | score      | ALL  | NULL          | NULL | NULL    | NULL |   15 | NULL                            |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
2 rows in set (0.04 sec)


-- 写在最后, 这篇文章和上一篇文章是有关系的, 
-- 区别点:
-- 1  分组求和的方式不同
-- 2  有一个行转列的过程.






 

转载于:https://my.oschina.net/u/1579560/blog/803577

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值