两个秒之间的时间戳之间的MySql区别? [英] MySql difference between two timestamps in Seconds?
本文介绍了两个秒之间的时间戳之间的MySql区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在Mysql中计算两个时间戳之间的差异并以秒为单位获取输出结果?像2010-11-29 13:16:55-2010-11-29 13:13:55应该花180秒.
Is it possible to calculate difference between two timestamps in Mysql and get output result in seconds? like 2010-11-29 13:16:55 - 2010-11-29 13:13:55 should give 180 seconds.
谢谢
推荐答案
Use the UNIX_TIMESTAMP function to convert the DATETIME into the value in seconds, starting from Jan 1st, 1970:
SELECT UNIX_TIMESTAMP('2010-11-29 13:16:55') - UNIX_TIMESTAMP('2010-11-29 13:13:55') as output
结果:
output
-------
180
An easy way to deal with if you're not sure which value is bigger than the other -- use the ABS function:
SELECT ABS(UNIX_TIMESTAMP(t.datetime_col1) - UNIX_TIMESTAMP(t.datetime_col2)) as output
这篇关于两个秒之间的时间戳之间的MySql区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文