所以我在MySQL中有一个表PowerUse。它有两个重要的列,ID(long int)和timeRecorded(timeStamp)。条目之间通常存在长时间差距。我的目标是查询表格以获得结果告诉我20分钟内每分钟有多少记录 - 包括0分钟没有记录的分数。
感谢MySQL: Select All Dates In a Range Even If No Records Present我已经得到了下面的查询,但是我无法让这件事情发挥作用。有人能指出我正在制造的明显错误,所以我可以不停地撞击砖墙吗?我知道它在JOIN中,我只是看不到它。谢谢!
select count(PowerUse.id) + 0 as counter, timestamp((PowerUse.timeRecorded div 100)*100) as time
from PowerUse right join
(
select date_add(NOW(), INTERVAL -n2.num*10-n1.num MINUTE) as dateb from
(select 0 as num
union all select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9) n1,
(select 0 as num
union all select 1
union all select 2
) n2
as ) datea
on (timestamp(PowerUse.timeRecorded div 100)*100) = dateb.datea
group by timeRecorded div 100 ;
答案 0 :(得分:1)
我猜您最好的选择是创建另一个表:
并用你的范围填充....
然后你可以加入:
select start_date, count(PowerUse.id) + 0 as counter, timestamp((PowerUse.timeRecorded div 100)*100) as time
from PowerUse
right join timeframes on ( timestamp((PowerUse.timeRecorded div 100)*100) >= start_date and timestamp((PowerUse.timeRecorded div 100)*100) < end_date )
group by start_date
像这样的东西