This question already has an answer here:
这个问题在这里已有答案:
- MySQL: how to get the difference between two timestamps in seconds 4 answers
- MySQL:如何获得两个时间戳之间的差异,以秒为单位4个答案
I have this table but I am helpless how to calculate seconds for each separate day from this table:
我有这张桌子,但我很无奈如何从这张桌子计算每一天的秒数:
id from to
---------------------------------------------------
1 2013-01-31 23:50:00 2013-02-02 09:00:00
2 2013-02-05 11:21:12 2013-02-08 01:01:01
3 2013-02-08 17:33:44 2013-02-08 18:22:55
4 2013-02-12 01:40:12 2013-02-12 02:00:59
5 2013-02-28 01:40:12 2013-03-02 02:00:59
Now I need to gain number of seconds for each day in february between from and to. so for 1st Feb - x seconds, 2nd Feb - y seconds, 3rd Feb - 0 seconds, etc. Any idea how should I do it? Many thanks.
现在我需要在2月到之间每天获得秒数。所以对于2月1日 - x秒,2月2日 - y秒,2月3日 - 0秒等等。任何想法我应该怎么做?非常感谢。
1 个解决方案
#1
26
Use TIMESTAMPDIFF MySQL.
使用TIMESTAMPDIFF MySQL。
SELECT TIMESTAMPDIFF(SECOND,from,to);
Example:
例:
SELECT TIMESTAMPDIFF(SECOND,'2016-01-01 00:00:00','2016-01-11 00:00:00'); -- 864000
SELECT TIMESTAMPDIFF(MINUTE,'2016-01-01 00:00:00','2016-01-11 00:00:00'); -- 14400
SELECT TIMESTAMPDIFF(HOUR,'2016-01-01 00:00:00','2016-01-11 00:00:00'); -- 240
SELECT TIMESTAMPDIFF(DAY,'2016-01-01 00:00:00','2016-01-11 00:00:00'); -- 10
#1
26
Use TIMESTAMPDIFF MySQL.
使用TIMESTAMPDIFF MySQL。
SELECT TIMESTAMPDIFF(SECOND,from,to);
Example:
例:
SELECT TIMESTAMPDIFF(SECOND,'2016-01-01 00:00:00','2016-01-11 00:00:00'); -- 864000
SELECT TIMESTAMPDIFF(MINUTE,'2016-01-01 00:00:00','2016-01-11 00:00:00'); -- 14400
SELECT TIMESTAMPDIFF(HOUR,'2016-01-01 00:00:00','2016-01-11 00:00:00'); -- 240
SELECT TIMESTAMPDIFF(DAY,'2016-01-01 00:00:00','2016-01-11 00:00:00'); -- 10