简体   繁体   English

DATEDIFF可以在几小时内获得差异

[英]DATEDIFF to get difference in hours

I have two columns that have time data that looks like "2017-12-06 18:50:27 +0000" . 我有两列时间数据看起来像“2017-12-06 18:50:27 +0000”。 What MySQL function can I use to get the difference in hours of the two date time strings? 我可以用什么MySQL函数来获得两个日期时间字符串的小时差异? I tried DATEDIFF(time_string_1, time_string_2) but it isn't working. 我尝试了DATEDIFF(time_string_1, time_string_2)但它无法正常工作。 What else can I use? 我还能用什么?

You can use the timestampdiff function: 您可以使用timestampdiff函数:

SELECT TIMESTAMPDIFF(HOUR, col1, col2)
FROM   mytable

Use TIMESTAMPDIFF function. 使用TIMESTAMPDIFF函数。

Syntex: 西尔文特克斯:

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)

Where Unit is: 单位是:

Microseconds, Seconds, Minutes, Hours, Days, Weeks, Months, Quarters, Years.

Query: 查询:

select TIMESTAMPDIFF(HOUR, time_string_1, time_string_2) 
from table_name

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

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