如何理解TIMESTAMP之间的减号运算符

时间:2017-06-21 15:58:05

标签: mysql timestamp

mysql> select timestamp('2017-06-21 23:45:01') - timestamp('2017-06-21 23:44:58');
+---------------------------------------------------------------------+
| timestamp('2017-06-21 23:45:01') - timestamp('2017-06-21 23:44:58') |
+---------------------------------------------------------------------+
|                                                                  43 |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select version();
+------------+
| version()  |
+------------+
| 5.7.16-log |
+------------+
1 row in set (0.00 sec)

显然,它不是timestamp计算的。请指教。

1 个答案:

答案 0 :(得分:1)

您必须使用TIMEDIFF()TIMESTAMPDIFF()

<强> DEMO

SELECT TIMEDIFF(timestamp('2017-06-21 23:45:00') , timestamp('2017-06-21 23:44:58'));
-- result: 00:00:02.

SELECT TIMESTAMPDIFF(SECOND,'2017-06-21 23:45:00','2017-06-21 23:44:58'); 
-- result: -2