SELECT TIMEDIFF( x.time_col, y.timecol ) FROM
( SELECT time_col FROM [tablename] ORDER BY [primary_key] DESC LIMIT 1 ) x,
( SELECT time_col FROM [tablename] ORDER BY [primary_key] ASC LIMIT 1 ) y
您也可以使用 TIMESTAMPDIFF 函数。
SELECT TIMESTAMPDIFF( SECOND, x.time_col, y.timecol ) FROM
( SELECT time_col FROM [tablename] ORDER BY [primary_key] DESC LIMIT 1 ) x,
( SELECT time_col FROM [tablename] ORDER BY [primary_key] ASC LIMIT 1 ) y
SELECT TIMEDIFF( MAX(time_col), MIN(time_col) ) FROM [tablename];
This will be helpful when it stores in time-sequentially. But timestamp column can be modified by updating, so I suggest the below query than above one.
SELECT TIMEDIFF( x.time_col, y.timecol ) FROM
( SELECT time_col FROM [tablename] ORDER BY [primary_key] DESC LIMIT 1 ) x,
( SELECT time_col FROM [tablename] ORDER BY [primary_key] ASC LIMIT 1 ) y
You can use TIMESTAMPDIFF function either.
SELECT TIMESTAMPDIFF( SECOND, x.time_col, y.timecol ) FROM
( SELECT time_col FROM [tablename] ORDER BY [primary_key] DESC LIMIT 1 ) x,
( SELECT time_col FROM [tablename] ORDER BY [primary_key] ASC LIMIT 1 ) y
发布评论
评论(2)
当它按时间顺序存储时,这将很有帮助。但是时间戳列可以通过更新来修改,所以我建议使用下面的查询而不是上面的查询。
您也可以使用 TIMESTAMPDIFF 函数。
This will be helpful when it stores in time-sequentially. But timestamp column can be modified by updating, so I suggest the below query than above one.
You can use TIMESTAMPDIFF function either.
这个例子是在 php 中使用 mysql_* 函数,
您可以替换为您喜欢的任何方法
this example is using mysql_* function in php,
you can replace to whatever method you are comfortable with