Datediff seconds return error

时间:2016-10-09 15:52:50

标签: mysql datediff

I have a variable $date="2016-10-09 10:44:03"; and a list of messages ordered by date_post ( TIMESTAMP CURRENT_TIMESTAMP). ($date is always lower than date_post for seconds)

I want to return results from

SELECT *,DATEDIFF(second, $date,date_post) AS duration 
FROM messages 
WHERE condition

but it returns error

supplied argument is not a valid MySQL result resource in ....

It is suppossed to return a number, but it just return error. According to the sintax, $date must be written first in DATEDIFF. I tried to use CAST($date), but nothing. Do I need to modify $date before using it? If I work with DATEDIFF(date_post,date_post) returns 0, but with $date returns error

1 个答案:

答案 0 :(得分:0)

如果您想要以秒为单位的差异,最简单的方法是使用to_seconds()

SELECT m.*,
       ( to_seconds(date_post) - to_seconds($date) )AS duration 
FROM messages m
WHERE condition;

您正在使用的功能是SQL Server功能。在MySQL中,datediff()返回两个日期之间的差异。