是否有可能获得 Sqlite3 中两个 TIMESTAMP 值之间的差异(以秒为单位)?
例如,我尝试了以下查询:
SELECT CURRENT_TIMESTAMP - my_timestamp FROM my_table;
我总是得到'0'。谁能告诉我我做错了什么?(注意,我已经验证 my_timestamp 确实是过去的。)
是否有可能获得 Sqlite3 中两个 TIMESTAMP 值之间的差异(以秒为单位)?
例如,我尝试了以下查询:
SELECT CURRENT_TIMESTAMP - my_timestamp FROM my_table;
我总是得到'0'。谁能告诉我我做错了什么?(注意,我已经验证 my_timestamp 确实是过去的。)
Got it:
SELECT (julianday(CURRENT_TIMESTAMP) - julianday(my_timestamp)) * 86400.0) FROM my_table;
julianday
returns the fractional number of days since noon in Greenwich on November 24, 4714 B.C. I then take the difference and multiply by the number of seconds per day.
最接近秒的另一种变体
CAST(strftime('%s', CURRENT_TIMESTAMP) as integer) -
CAST(strftime('%s', my_timestamp) as integer)