本文介绍了mysql timediff到小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从表格中获取timediff并将其转换为小时(用于按小时计费的服务)
I'm Trying to get the timediff from my table and convert it to hours (it's for an hourly billed service)
其中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
或者, TimestampDiff 也可以以更优雅的方式提供有效的解决方案,并提供示例:
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到小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!