我正在寻找一种方法来获取按 MySQL 中涉及的天数分组的两个时间戳的 TIMEDIFF。
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函数中进行分组?
最佳答案
为了做你想做的事,你需要生成一个数字序列。也许你有一张 table 。如果不是,则类似于以下内容:
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
(未经测试,可能存在语法错误。)
关于php - TIMEDIFF GROUP BY 涉及天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14490060/