我有两次start_time和end_time,我希望得到start_time和end_time之间的秒数差异,意味着如果start_time = 1:1:0和end_time = 1:2:3那么我应该得到63。 请告诉我应该为此申请MySQL的查询。
答案 0 :(得分:1)
TIMESTAMPDIFF()
会为您提供所需内容:
SELECT TIMESTAMPDIFF(SECOND, start_time, end_time) as timediff from `sometable` WHERE...
MySQL参考:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampdiff
答案 1 :(得分:0)
我会对此发表评论,但刚加入。在他的要求中,开始时间是过去的,但结果是正时间差。所以
SELECT TIMESTAMPDIFF(SECOND, start_time, end_time)...
应该是
SELECT ABS(TIMESTAMPDIFF(SECOND, start_time, end_time))...;
完整示例
SET @startDate = NOW() - INTERVAL 63 SECOND;
SET @endDate = NOW();
SELECT ABS(TIMESTAMPDIFF(SECOND, @startDate, @endDate)) AS time_difference;
-- or this
SELECT ABS(TO_SECONDS(@startDate) - TO_SECONDS(@endDate)) AS time_difference;