Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.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_Gaps And Islands - Fatal编程技术网

MySQL如何填写范围内缺失的小时/日期?

MySQL如何填写范围内缺失的小时/日期?,mysql,gaps-and-islands,Mysql,Gaps And Islands,我试图在同一个声明中根据一周中的哪一天和哪一天的哪一小时提取数据(这样我就可以看到我每小时和一周中的访问量了。下面是声明 SELECT count(id) as count, HOUR(created) as hour_of_day, WEEKDAY(created) as day_of_week, DATE_FORMAT(created,'%W') name_of_day FROM visitors GROUP BY day_of_week,hour_of_day ORDER BY day_o

我试图在同一个声明中根据一周中的哪一天和哪一天的哪一小时提取数据(这样我就可以看到我每小时和一周中的访问量了。下面是声明

SELECT
count(id) as count,
HOUR(created) as hour_of_day,
WEEKDAY(created) as day_of_week,
DATE_FORMAT(created,'%W') name_of_day
FROM visitors
GROUP BY day_of_week,hour_of_day
ORDER BY day_of_week,hour_of_day ASC
一些样本数据

    count   hour_of_day day_of_week name_of_day
    2       0           0           Monday
    1       1           0           Monday
    1       4           0           Monday
    4       5           0           Monday
    1       6           0           Monday
    4       7           0           Monday
    1       9           0           Monday
    1       10          0           Monday
    1       12          0           Monday
    1       13          0           Monday
    2       16          0           Monday
    5       18          0           Monday
    5       19          0           Monday
问题 如您所见,数据中有许多小时缺少数据。我正在创建一个图表,该图表需要每天以[x,x,x,x,x,x]的形式提供数据,该图表将与从第一次输出开始的24小时时间线相匹配,我需要缺少的数据为“0”

虽然我可以通过循环在PHP端处理它,但是一周中的每一天,每一个小时,循环它都是相当烦人的,而且绝对不干净


是否可以不使用临时表(如在查询本身中包含24位数字等?

不确定为什么不使用临时表,这会使生活更轻松

最佳解决方案如下所示:


基本上,您必须创建一个包含一天中所有时间的表,并将join保留在该表上。

不是最漂亮的。但如果您真的不能使用临时表,它应该可以做到这一点:

select ifnull(count,0) as count,dh.hour_of_day,
dh.day_of_week,date_format((date('2012-01-02') + interval dh.day_of_week day),'%W') as name_of_day
from
(
select day_of_week,hour_of_day
from 
(
 select 0 as day_of_week union select 1 union select 2 union select 3 
 union select 4 union select 5 union select 6
) d
 join
(
 select 0 as hour_of_day 
 union select 1 union select 2 union select 3 union select 4 
 union select 5 union select 6 union select 7 union select 8
 union select 9 union select 10 union select 11 union select 12
 union select 13 union select 14 union select 15 union select 16
 union select 17 union select 18 union select 19 union select 20
 union select 21 union select 22 union select 23
) h
) dh
left outer join
(
SELECT
count(id) as count,
HOUR(created) as hour_of_day,
WEEKDAY(created) as day_of_week,
DATE_FORMAT(created,'%W') name_of_day
FROM visitors
GROUP BY day_of_week,hour_of_day
) v on dh.day_of_week = v.day_of_week and dh.hour_of_day = v.hour_of_day
ORDER BY dh.day_of_week,dh.hour_of_day ASC; 

小心这一点!如果你在多个星期内运行查询,那么一周中的多天会被加在一起。你可能想考虑添加一个“仅这个星期”的谓词。例如,添加in <代码>哪里是年份(创建)=年份(现在())。进入您的原始选择,仅获取本周的数据。

虽然这可以从理论上回答问题,但在此处包含答案的基本部分,并提供链接供参考。(另外:链接已断开)断开的链接==错误答案