2013-01-23 101 views
1

我正在寻找一种方法来获取两个时间戳的TIMEDIFF,这些时间戳是通过MySQL中涉及的日期分组的。TIMEDIFF GROUP BY涉及日期

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和2,2和3等)? – pzirkind

+0

不,我只有两个时间戳timestamp1(2012-01-01 ...)和timestamp2(2012-01-09 ...)。结果应该是1月1日至9日的日期以及比例小时/分钟/秒。 – Thomas1703

+2

啊,请在问题中发布所需的输出,以便我们可以更好地为您提供帮助 – pzirkind

回答

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 

(这不是测试,所以它可能有语法错误。)