简体   繁体   中英

Why does the minus operator give different result than the TIMESTAMPDIFF() function in mysql?

Since TIMESTAMP in mysql is stored as a 32bit value representing the time interval from 1970-jan-1 0:00:00 in seconds, I assumed that using minus (-) operator on TIMESTAMP values would give the difference of these values in seconds. Actually not:

+---------------------------------------------------------------------+
| TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:29:59") |
+---------------------------------------------------------------------+
| 41.000000                                                           |
+---------------------------------------------------------------------+
1 row in set (0.05 sec)

mysql> select timestampdiff(SECOND,TIMESTAMP("2010-04-02 10:30:00"),TIMESTAMP("2010-04-02 10:29:59"));
+-----------------------------------------------------------------------------------------+
| timestampdiff(SECOND,TIMESTAMP("2010-04-02 10:30:00"),TIMESTAMP("2010-04-02 10:29:59")) |
+-----------------------------------------------------------------------------------------+
| -1                                                                                      |
+-----------------------------------------------------------------------------------------+

mysql> select TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:30:01") ;
+---------------------------------------------------------------------+
| TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:30:01") |
+---------------------------------------------------------------------+
| -1.000000                                                           |
+---------------------------------------------------------------------+

+---------------------------------------------------------------------+
| TIMESTAMP("2010-04-02 10:30:00") - TIMESTAMP("2010-04-02 10:31:00") |
+---------------------------------------------------------------------+
| -100.000000                                                         |
+---------------------------------------------------------------------+

It seems like one minute difference is 100 instead of 60.

Why is this?

Just a wild guess, but maybe you're casting the strings to an integer in three of the cases?

20100402103000 - 20100402103100 = -100

20100402103000 - 20100402103001 = -1

20100402103000 - 20100402102959 = 41

The other case does the conversion properly.

The correct function to use is UNIX_TIMESTAMP() .

TIMESTAMP() returns a date(time), in the format '2003-12-31 00:00:00'.

Also, there is nothing wrong with using

SELECT TIMESTAMPDIFF(SECOND,NOW(),TIMESTAMP("2010-04-02 19:29:59"));

I tried the following and received NULL

SELECT TIMESTAMPDIFF(SECOND, TIMESTAMP("stopTime"), TIMESTAMP("startTime")) FROM 
paktime WHERE fs = 1 AND employ LIKE "Mike Fowler"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM