简体   繁体   English

如何在mysql查询中找到2个日期的小时总数

[英]how to find the total numbers of hours of 2 dates in mysql query

i have this table called records: 我有此表称为记录:

+---------------------+---------------------+
| date_returned       | date_borrowed       |
+---------------------+---------------------+
| 2013-05-31 09:27:38 | 2013-05-31 08:48:14 |
| 2013-05-31 09:22:27 | 2013-05-31 08:53:06 |
| 2013-05-31 09:27:38 | 2013-05-31 09:22:35 |
| 2013-05-31 15:27:02 | 2013-05-31 09:22:39 |
| 2013-05-31 15:27:02 | 2013-05-31 10:04:22 |
+---------------------+---------------------+

i want to include the total hours as another column in the result. 我想将总时数作为结果的另一列。 i read the manual but datediff seemed to be vague and just returns the difference as days. 我阅读了手册,但datediff似乎含糊不清,只是将差额返回为几天。

Try: 尝试:

SELECT 
    TIMEDIFF(date_borrowed, date_returned) AS diff
FROM
    tablename
SELECT 
    HOUR(TIMEDIFF(date_borrowed, date_returned)) as TotalHours
FROM
    tablename

How about converting to timestamp values - that'll let you subtract to get the difference in seconds, and you can manipulate as much as you want from there. 如何转换为时间戳值-可以减去以秒为单位的差异,并且可以从那里进行任意操作。

SELECT
    date_returned,
    date_borrowed,
    (UNIX_TIMESTAMP(date_returned) - UNIX_TIMESTAMP(date_borrowed)) / 3600 AS hours
FROM tablename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM