[英]MYSQL Workbench. How can I calculate age in sql (mysql workbench)? Then, find the ratio of age in database
[英]How can I find the age of a record in minutes when using MySQL?
我有一個歷史表,其中的datetime
字段名為createdAt
。 我正在嘗試確定記錄的年齡,但是age列返回為Null
:
SELECT id, (MINUTE(TIMEDIFF(NOW(),IFNULL(history.createdAt,0)))) AS age
FROM history
WHERE history.type = @type
我嘗試使用DATEDIFF()
而不是TIMEDIFF()
並得到0
知道為什么該查詢的行為不符合預期嗎?
更新:
內省數據庫后,我發現createdAt
列是date
而不是datetime
列。 如果使用datetime
列,代碼將按預期工作。
目前無法測試,但API表示:
返回時間的分鍾,范圍為0到59。
mysql> SELECT MINUTE('2008-02-03 10:05:03');
-> 5
http://dev.mysql.com/doc/refman/5.5/zh-CN/date-and-time-functions.html#function_minute
-不是您想做的。 對TIMEDIFF的結果嘗試TIME_TO_SEC(),並將其除以60得到分鍾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.