How can I find the age of a record in minutes when using MySQL?
I have a history table with a datetime
field named createdAt
. I'm trying to determine the age of the record but the age column comes back as Null
:
SELECT id, (MINUTE(TIMEDIFF(NOW(),IFNULL(history.createdAt,0)))) AS age
FROM history
WHERE history.type = @type
I tried DATEDIFF()
instead of TIMEDIFF()
and I get a 0
back
Any idea why this 开发者_StackOverflow社区query is not behaving as expected?
UPDATE:
After introspecting the database, I found that the createdAt
column was a date
and not a datetime
column. The code works as expected if using a datetime
column.
Not able to test right now, but the API says:
Returns the minute for time, in the range 0 to 59.
mysql> SELECT MINUTE('2008-02-03 10:05:03');
-> 5
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_minute
-- not what you want to do. Try TIME_TO_SEC() on the result from TIMEDIFF, and divide it by 60 to get minutes.
精彩评论