1

我正在寻找一种方法来获取在 MySQL 中按相关天数分组的两个时间戳的 TIMEDIFF。

SELECT TIMEDIFF('2012-01-02 10:00:00', '2012-01-01 10:00:00');

这只是给出 24 小时的差异。

我需要两天(或更多)的差异。IE:

2012-01-01: 14:00:00
2012-01-02: 10:00:00
[...]

有没有办法在 TIMEDIFF 函数中进行分组?

4

1 回答 1

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

(这未经测试,因此可能存在语法错误。)

于 2013-01-24T03:09:06.807 回答