如何获取 MYSQL 中两个日期时间之间的差异?

发布于 2024-10-22 17:33:32 字数 399 浏览 3 评论 0原文

我正在尝试此操作

SELECT DATEDIFF(second,log.start_time,log.end_time) 
as seconds 
from log 
where log.log_id = "some crazy UUID";

,但收到错误,因为 DATEDIFF() 不接受像第二这样的格式化程序 expr。

所以我尝试了,

SELECT second(DATEDIFF(second,log.start_time,log.end_time)) 
as seconds 
from log
where log.log_id = "some crazy UUID";

但这也不适用于格式化。

I'm trying this

SELECT DATEDIFF(second,log.start_time,log.end_time) 
as seconds 
from log 
where log.log_id = "some crazy UUID";

but I get an error because DATEDIFF() accepts no formatter expr like second.

So I tried,

SELECT second(DATEDIFF(second,log.start_time,log.end_time)) 
as seconds 
from log
where log.log_id = "some crazy UUID";

But that doesn't work for formatting either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

极致的悲 2024-10-29 17:33:32

我认为你打算使用 TIMESTAMPDIFF() 而不是 DATEDIFF()

SELECT TIMESTAMPDIFF(second,log.start_time,log.end_time) as seconds 
from log 
where log.log_id = "some crazy UUID";

I think you meant to use TIMESTAMPDIFF() instead of DATEDIFF():

SELECT TIMESTAMPDIFF(second,log.start_time,log.end_time) as seconds 
from log 
where log.log_id = "some crazy UUID";
温柔戏命师 2024-10-29 17:33:32

试试这个

SELECT TIME_TO_SEC( TIMEDIFF( log.start_time, log.end_time )) AS seconds
FROM log 
WHERE log.log_id = "some crazy UUID";

Try this

SELECT TIME_TO_SEC( TIMEDIFF( log.start_time, log.end_time )) AS seconds
FROM log 
WHERE log.log_id = "some crazy UUID";
混吃等死 2024-10-29 17:33:32

DATEDIFF 仅接受两个参数,而您在此处传递三个参数。

它是如何工作的,并且它只返回黑白日期的差异,不包括时间。

http://dev.mysql.com/doc /refman/5.1/en/date-and-time-functions.html

DATEDIFF accept only two parameters and you are passing three parameters here.

How it works and also it returns only the difference b/w dates not including time.

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

碍人泪离人颜 2024-10-29 17:33:32

DATEDIFF() 返回差异天数。

使用 TIMEDIFF() 或使用 TO_SECONDS() 转换两个时间戳并获取它们的差异:

SELECT TO_SECONDS(log.end_time) - TO_SECONDS(log.start_time) AS seconds
  FROM log 
  WHERE log.log_id = "some crazy UUID"
;

DATEDIFF() returns days of difference.

Use TIMEDIFF() or convert both timestamps with TO_SECONDS()) and get their difference:

SELECT TO_SECONDS(log.end_time) - TO_SECONDS(log.start_time) AS seconds
  FROM log 
  WHERE log.log_id = "some crazy UUID"
;
毁梦 2024-10-29 17:33:32

DATEDIFF

来自 msdn,它返回指定开始日期和结束日期之间跨越的指定日期部分边界的计数(有符号整数)。 DATEDIFF() 函数返回两个日期之间的时间。
语法:

DATEDIFF ( datepart , startdate , enddate )  

您应该使用 TIMEDIFF()TIMESTAMPDIFF() 函数。
只需将 TIMESTAMPDIFF() 函数替换为 DATEFDIFF()

DATEDIFF :

From the msdn, it returns the count (signed integer) of the specified datepart boundaries crossed between the specified startdate and enddate. The DATEDIFF() function returns the time between two dates.
Syntax :

DATEDIFF ( datepart , startdate , enddate )  

You should use TIMEDIFF() or TIMESTAMPDIFF() function.
Just replace TIMESTAMPDIFF() function with DATEFDIFF()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文