我正在寻找一种方法来获取两个时间戳的TIMEDIFF,这两个时间戳按MySQL中涉及的天数分组。
SELECT TIMEDIFF('2012-01-02 10:00:00', '2012-01-01 10:00:00');
这只是24小时的差异。
我需要两天(或更多)的差异。即:
2012-01-01: 14:00:00
2012-01-02: 10:00:00
[...]
有没有办法在TIMEDIFF函数中进行分组?
答案 0 :(得分:1)
为了做你想做的事,你需要生成一系列数字。也许你有一张桌子。如果没有,请执行以下操作:
select dstart + interval n.num days
from (select d1*10+d2 as num
from (select 0 as d 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
) d1 cross join
(select 0 as d 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
) d2
) n join
(select date('2012-01-02') as dend, date('2012-01-01') dstart
on dstart + interval n.num days <= dend
(这未经过测试,因此可能存在语法错误。)