mysqlgroupby范围_如何在MySQL中实现GROUPBY范围?为了在MySQL中按范围分组,让我们⾸先创建⼀个表。创建表的查询如下mysql> create table GroupByRangeDemo
- > (
- > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
- > YourRangeValue int
- > );
现在,您可以使⽤insert命令在表中插⼊⼀些记录。
查询如下mysql> insert into GroupByRangeDemo(YourRangeValue) values(1);
mysql> insert into GroupByRangeDemo(YourRangeValue) values(7);
mysql> insert into GroupByRangeDemo(YourRangeValue) values(9);
mysql> insert into GroupByRangeDemo(YourRangeValue) values(23);
mysql> insert into GroupByRangeDemo(YourRangeValue) values(33);
mysql> insert into GroupByRangeDemo(YourRangeValue) values(35);
mysql> insert into GroupByRangeDemo(YourRangeValue) values(1017);
使⽤select语句显⽰表中的所有记录。
查询如下mysql> select *from GroupByRangeDemo;
以下是输出+----+----------------+
| Id | YourRangeValue |
+----+----------------+
| 1 | 1 |
| 2 | 7 |
| 3 | 9 |
| 4 | 23 |
| 5 | 33 |
| 6 | 35 |
| 7 | 1017 |
+----+----------------+
7 rows in set (0.04 sec)
groupby分组这是按范围分组的查询
mysql> select round(YourRangeValue / 10), count(YourRangeValue) from GroupByRangeDemo where YourRangeValue
- > union
- > select '40+', count(YourRangeValue) from GroupByRangeDemo where YourRangeValue >= 40;
以下是输出+----------------------------+-----------------------+
| round(YourRangeValue / 10) | count(YourRangeValue) |
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论