Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 按范围选择和分组_Mysql_Sql - Fatal编程技术网

Mysql 按范围选择和分组

Mysql 按范围选择和分组,mysql,sql,Mysql,Sql,我有一些数据如下: +----+--------+-------+ | id | height | width | +----+--------+-------+ | 1 | 1000 | 300 | | 2 | 1024 | 330 | | 3 | 600 | 200 | | 4 | 624 | 311 | | 5 | 724 | 511 | | 6 | 300 | 200 | +----+--------+-------+

我有一些数据如下:

+----+--------+-------+
| id | height | width |
+----+--------+-------+
|  1 |   1000 |   300 |
|  2 |   1024 |   330 |
|  3 |    600 |   200 |
|  4 |    624 |   311 |
|  5 |    724 |   511 |
|  6 |    300 |   200 |
+----+--------+-------+
Count how many rows have a height between 0 and 400, between 401 and 600, between 601 and 1000, and 1000+
+-------+---------+----------+-------+
| 0-400 | 401-600 | 601-1000 | 1000+ |
+-------+---------+----------+-------+
|     1 |       1 |        3 |     1 |
+-------+---------+----------+-------+
select sum(if(height<400,1,0)) '<400', sum(if(height>=400 and height<600,1,0)) '400-600'...
有更多的行

我想运行一个查询,该查询执行以下操作:

+----+--------+-------+
| id | height | width |
+----+--------+-------+
|  1 |   1000 |   300 |
|  2 |   1024 |   330 |
|  3 |    600 |   200 |
|  4 |    624 |   311 |
|  5 |    724 |   511 |
|  6 |    300 |   200 |
+----+--------+-------+
Count how many rows have a height between 0 and 400, between 401 and 600, between 601 and 1000, and 1000+
+-------+---------+----------+-------+
| 0-400 | 401-600 | 601-1000 | 1000+ |
+-------+---------+----------+-------+
|     1 |       1 |        3 |     1 |
+-------+---------+----------+-------+
select sum(if(height<400,1,0)) '<400', sum(if(height>=400 and height<600,1,0)) '400-600'...
在这种情况下,我希望它返回如下内容:

+----+--------+-------+
| id | height | width |
+----+--------+-------+
|  1 |   1000 |   300 |
|  2 |   1024 |   330 |
|  3 |    600 |   200 |
|  4 |    624 |   311 |
|  5 |    724 |   511 |
|  6 |    300 |   200 |
+----+--------+-------+
Count how many rows have a height between 0 and 400, between 401 and 600, between 601 and 1000, and 1000+
+-------+---------+----------+-------+
| 0-400 | 401-600 | 601-1000 | 1000+ |
+-------+---------+----------+-------+
|     1 |       1 |        3 |     1 |
+-------+---------+----------+-------+
select sum(if(height<400,1,0)) '<400', sum(if(height>=400 and height<600,1,0)) '400-600'...
我将对范围进行硬编码


目前,我正计划为每个范围运行一个查询,有没有更好的方法?

尝试以下方法:

+----+--------+-------+
| id | height | width |
+----+--------+-------+
|  1 |   1000 |   300 |
|  2 |   1024 |   330 |
|  3 |    600 |   200 |
|  4 |    624 |   311 |
|  5 |    724 |   511 |
|  6 |    300 |   200 |
+----+--------+-------+
Count how many rows have a height between 0 and 400, between 401 and 600, between 601 and 1000, and 1000+
+-------+---------+----------+-------+
| 0-400 | 401-600 | 601-1000 | 1000+ |
+-------+---------+----------+-------+
|     1 |       1 |        3 |     1 |
+-------+---------+----------+-------+
select  sum(case when height between 0 and 400 then 1 end) as [0-400]
,       sum(case when height between 401 and 600 then 1 end) as [401-600]
,       ...
from    YourTable
select sum(if(height<400,1,0)) '<400', sum(if(height>=400 and height<600,1,0)) '400-600'...

选择sum(如果(高度=400,高度),尝试以下方法:

+----+--------+-------+
| id | height | width |
+----+--------+-------+
|  1 |   1000 |   300 |
|  2 |   1024 |   330 |
|  3 |    600 |   200 |
|  4 |    624 |   311 |
|  5 |    724 |   511 |
|  6 |    300 |   200 |
+----+--------+-------+
Count how many rows have a height between 0 and 400, between 401 and 600, between 601 and 1000, and 1000+
+-------+---------+----------+-------+
| 0-400 | 401-600 | 601-1000 | 1000+ |
+-------+---------+----------+-------+
|     1 |       1 |        3 |     1 |
+-------+---------+----------+-------+
select sum(if(height<400,1,0)) '<400', sum(if(height>=400 and height<600,1,0)) '400-600'...

select sum(if(height=400和heightThank,我知道这一定很简单!@RichBradshaw:实际上可能更容易。在MySQL中,布尔值在大多数(如果不是全部)上下文中都会隐式转换为整数。因此,您可以直接将布尔表达式用作
sum
s:
sum(高度介于0和400之间)的参数
SUM(height>1000)
等等。谢谢,我知道这必须很简单!@RichBradshaw:实际上更容易。在MySQL中,布尔值在大多数(如果不是全部)上下文中都会隐式转换为整数。因此,您可以直接将布尔表达式用作
SUM
s:
SUM(height介于0和400之间)的参数
总和(高度>1000)
等。