mysql timediff到小时 [英] mysql timediff to hours
问题描述
我正在尝试从表格中获取timediff并将其转换为小时(用于按小时计费的服务)
I'm Trying to get the timediff from my table and convert it to hours (it's for an hourly billed service)
SELECT TIME_TO_SEC(TIMEDIFF(endDate,startDate))/3600 FROM任务>
SELECT TIME_TO_SEC(TIMEDIFF(endDate,startDate))/3600 FROM tasks >
其中endDate和startDate为日期时间格式
where endDate and startDate are in datetime format
还有另一种方式(更有效)来完成此任务吗? 谢谢!
is there another way (more efficient) to do this task? Thanks !
推荐答案
TIMEDIFF(endDate, startDate)
以DateTime格式输出,以平坦的格式进行时间戳记并以(60 * 60)表示
TIMEDIFF(endDate, startDate)
outputs in DateTime format, so flat that to timestamp and devide by (60*60)
SELECT (UNIX_TIMESTAMP(TIMEDIFF(endDate, startDate))/(60*60)) AS hours_difference
FROM tasks
Alternatively,TimestampDiff may also provide a valid solution in more elegant way providing its example:
SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
您的解决方案可以是:
SELECT TIMESTAMPDIFF(HOUR, startDate, endDate) AS hours_different
FROM tasks
这篇关于mysql timediff到小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!