Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.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/9/silverlight/4.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,我有一个包含DATETIME类型字段的表。我需要根据一天中的小时数生成记录计数的柱状图。我使用以下查询获得直方图: select HOUR(date) AS `hour`, count(*) as count from records group by hour(date); 结果: + --------- + ---------- + | 5 | 7 | | 9 | 10 | | 10 | 2131

我有一个包含DATETIME类型字段的表。我需要根据一天中的小时数生成记录计数的柱状图。我使用以下查询获得直方图:

select HOUR(date) AS `hour`, count(*) as count from records group by hour(date);
结果:

+ --------- + ---------- +
| 5         | 7          |
| 9         | 10         |
| 10        | 2131       |
| 12        | 14         |
+ --------- + ---------- +
4 rows
所以,在所有日期的05:00:00和05:59:59之间有7条记录,以此类推。但在早上6点没有记录。我希望结果中的所有值都在00和23之间,且值为0:

+ --------- + ---------- +
| 0         | 0          |
| 1         | 0          |
| 2         | 0          |
| 3         | 0          |
| 4         | 0          |
| 5         | 7          |
| 6         | 0          |
| 7         | 0          |
          .....
24 rows

我已经做了很多类似于您的直方图查询,到目前为止,我找到的唯一没有临时性的方法是对结果数组进行foreach,并用缺少的值来完成它

下面是一个php示例。 请注意,您必须根据您的代码调整请求

//query my db and return an array with values only on hour that have a value $query = 'SELECT HOUR(date) AS `hour`, count(*) as count FROM records GROUP BY HOUR(date)'; //PDO stuff $query->execute(); $queryResult = $query->fetchall(); print_r($queryResult); // Array ( [0] => Array ( [hour] => 6 [count] => 1 ) [1] => Array ( [hour] => 7 [count] => 1 )) $H24 = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23); //foreach on the queryResult and add missing hour with a default value foreach ($H24 as $key=>$hour) { if(!in_array($hour, $queryResult)) array_push($queryResult, array('hour' => $hour, 'count' => '0')); } //at this point $quertResult now have a value for each hour print_r($queryResult); //Array ( [0] => Array ( [hour] => 0 [count] => 0 ) [1] => Array ( [hour] => 1 [count] => 1 ... [x] => Array ( [hour] => 23 [count] => 0 )) //查询my db并返回一个数组,该数组的值仅在小时内有一个值 $query='选择小时(日期)作为'HOUR',按小时(日期)从记录组中计数(*)'; //PDO材料 $query->execute(); $queryResult=$query->fetchall(); 打印(查询结果); //数组([0]=>Array([hour]=>6[count]=>1)[1]=>Array([hour]=>7[count]=>1)) $H24=数组(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23); //在queryResult上foreach并使用默认值添加缺少的小时 foreach($H24作为$key=>$hour){ if(!in_数组($hour,$queryResult)) 数组推送($queryResult,数组('hour'=>$hour,'count'=>'0'); } //此时$quertResult现在每小时有一个值 打印(查询结果); //数组([0]=>Array([hour]=>0[count]=>0)[1]=>Array([hour]=>1[count]=>1…[x]=>Array([hour]=>23[count]=>0))
希望能有所帮助

我在这里创建了一个SQLFIDLE:

select HOUR(date) AS `hour`, 
count(*) - 1 as count 
from 
(select * 
 from records
union
select "2008-01-19 01:14:07", 0
union
select "2008-01-19 02:14:07", 0
union
select "2008-01-19 03:14:07", 0
union
select "2008-01-19 04:14:07", 0
union
select "2008-01-19 05:14:07", 0
union
select "2008-01-19 06:14:07", 0
union
select "2008-01-19 07:14:07", 0
union
select "2008-01-19 08:14:07", 0
union
select "2008-01-19 09:14:07", 0
union
select "2008-01-19 10:14:07", 0
union
select "2008-01-19 11:14:07", 0
union
select "2008-01-19 12:14:07", 0
union
select "2008-01-19 13:14:07", 0
union
select "2008-01-19 14:14:07", 0
union
select "2008-01-19 15:14:07", 0
union
select "2008-01-19 16:14:07", 0
union
select "2008-01-19 17:14:07", 0
union
select "2008-01-19 18:14:07", 0
union
select "2008-01-19 19:14:07", 0
union
select "2008-01-19 20:14:07", 0
union
select "2008-01-19 21:14:07", 0
union
select "2008-01-19 22:14:07", 0
union
select "2008-01-19 23:14:07", 0) as b
group by hour(date);