2012-01-04 128 views
1

我试图根据星期几的&还小时一天在同一个语句的(这样我就可以看到我每小时超过一个星期有多少人访问了提取数据,以下声明。MySQL如何填补范围内缺失的小时/日期?

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,x],这将与24小时时间轴st相匹配从第一个输出创作出来,我需要缺失的那个为'0'。

虽然我可以在PHP端使用循环来处理它,但在每周的每一天都会循环使用它,因为每个小时都是相当烦人的,而且绝对不干净。

是否有可能没有临时表(如包括查询本身的24位数字?)?

回答

2

不是最漂亮的。但它应该做的伎俩,如果你真的不能使用临时表:

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; 

虽然小心!如果您在多个星期内运行查询,那么一周中的多天将被添加到一起。您可能需要考虑添加“仅本周”谓词。例如,将where yearweek(created) = yearweek(now())添加到原始选择中即可获取当前星期的数据。

2

不知道为什么你不想使用临时表,它使生活变得更容易。

的解决方案是最好的位置布局:http://www.freeopenbook.com/mysqlcookbook/mysqlckbk-chp-12-sect-10.html

从本质上讲,你需要创建一个表,在一天的所有时间和左的连接就可以了。

+1

虽然这可能在理论上回答这个问题,但[这将是更可取的](http://meta.stackexchange.com/q/8259)在这里包括答案的基本部分,并提供参考链接。 (另外:链接被破坏) – Nanne 2013-06-25 11:41:17