我将在下面的示例中说明我想要获得的内容:
'2010-09-01 03:00:00' - '2010-09-01 00:10:00'
Run Code Online (Sandbox Code Playgroud)
使用TIMEDIFF(),我们得到2结果.这意味着,它没有考虑剩下的50分钟.
在这种情况下,我想得到的是:50(分钟)/ 60 = 0.83期间.因此,结果应该是2.83而不是2.
Mat*_*hew 103
select time_to_sec(timediff('2010-09-01 03:00:00', '2010-09-01 00:10:00' )) / 3600;
+-----------------------------------------------------------------------------+
| time_to_sec(timediff('2010-09-01 03:00:00', '2010-09-01 00:10:00' )) / 3600 |
+-----------------------------------------------------------------------------+
| 2.8333 |
+-----------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
ria*_*hc3 13
我认为更完整的答案是
select time_format(timediff(onedateinstring,anotherdateinstring),'%H:%i:%s')
Run Code Online (Sandbox Code Playgroud)
这允许您以您想要的格式查看您希望看到的小时,分钟,秒等...
有关time_format的更多信息:http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_time-format
小智 11
您可以尝试以下方法:
time_format(timediff(max(l.fecha), min(l.fecha) ),'%H:%m') //Hora y minutos
Run Code Online (Sandbox Code Playgroud)
小智 10
使用TIMESTAMPDIFF确切的小时数:
SELECT
b.`Start_Date`,
b.`End_Date`,
TIMESTAMPDIFF(HOUR, b.`Start_Date`, b.`End_Date`) AS `HOURS`
FROM my_table b;
Run Code Online (Sandbox Code Playgroud)
它是一个非常老的线程,但您也可以尝试TIMESTAMPDIFF并将MINUTE,HOUR,SECONDS作为限定符.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timestampdiff
MySQL 获取时间戳之间的小时数:
select timestampdiff(second,'2010-09-01 00:10:00', '2010-09-01 03:00:00')/3600;
Run Code Online (Sandbox Code Playgroud)