博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql 范围统计(-1)
阅读量:6545 次
发布时间:2019-06-24

本文共 2717 字,大约阅读时间需要 9 分钟。

hot3.png

-- 创建数据表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_classFROM    (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)-- indexmysql> 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     | 
| 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

你可能感兴趣的文章
安装 GNU gcc 编译器、g++ 编译器、make 和 gdb (CYGWIN)
查看>>
汇编字符串拷贝
查看>>
Lambda的前世今生
查看>>
黑马程序员-张老师基础加强3-内省
查看>>
TCP/IP模型简介和/etc/hosts文件说明
查看>>
UIButton常用属性
查看>>
主键自增归0
查看>>
杨辉三角
查看>>
mysql之 [ERROR] InnoDB: Unable to lock ./ibdata1, error: 11
查看>>
如何批量修改文件后缀的方法
查看>>
Effective STL 笔记
查看>>
[LeetCode] 1. Two Sum
查看>>
超时时间已到。在操作完成之前超时时间已过或服务器未响应。 (.Net SqlClient Data Provider)(转)...
查看>>
POJ2538 ZOJ1884 UVA10082 WERTYU【输入输出】
查看>>
HDU5620 KK's Steel(C++语言版)
查看>>
旋转卡壳
查看>>
2016/10/09
查看>>
自定义HorizontalScrollView的scrollBar
查看>>
c++学习笔记和思考
查看>>
27.Docker集群部署
查看>>