mysql時間相減的問題 今天看到寧青同學的一條微博,提到mysql日期相減的錯誤結果,以前沒有怎麼注意,於是測試了一下,發現確實很坑爹,很容易踩雷,於是整理博客提醒一下大家。 先看一下錯誤的現象如下,第1條正確,第2,3條的t2-t1不正確: [sql] mysql> select t1,t2,t2-t1 from mytest; +---------------------+---------------------+-------+ | t1 | t2 | t2-t1 | +---------------------+---------------------+-------+ | 2013-04-21 16:59:33 | 2013-04-21 16:59:43 | 10 | | 2013-04-21 16:59:33 | 2013-04-21 17:00:33 | 4100 | | 2013-04-21 16:59:33 | 2013-04-21 17:59:35 | 10002 | +---------------------+---------------------+-------+ 3 rows in set 全部測試腳本如下: [sql] --創建表 mysql> CREATE TABLE mytest ( t1 datetime, t2 datetime ); Query OK, 0 rows affected --插入測試記錄 mysql> insert into mytest(t1,t2) values('2013-04-21 16:59:33','2013-04-21 16:59:43'); Query OK, 1 row affected mysql> insert into mytest(t1,t2) values('2013-04-21 16:59:33','2013-04-21 17:00:33'); Query OK, 1 row affected mysql> insert into mytest(t1,t2) values('2013-04-21 16:59:33','2013-04-21 17:59:35'); Query OK, 1 row affected --驗證結果 mysql> select t1,t2,t2-t1 from mytest; +---------------------+---------------------+-------+ | t1 | t2 | t2-t1 | +---------------------+---------------------+-------+ | 2013-04-21 16:59:33 | 2013-04-21 16:59:43 | 10 | | 2013-04-21 16:59:33 | 2013-04-21 17:00:33 | 4100 | | 2013-04-21 16:59:33 | 2013-04-21 17:59:35 | 10002 | +---------------------+---------------------+-------+ 3 rows in set 實際是mysql的時間相減是做了一個隱式轉換操作,將時間轉換為整數,但並不是用unix_timestamp轉換,而是直接把年月日時分秒拼起來,如2013-04-21 16:59:33 直接轉換為20130421165933,由於時間不是十進制,所以最後得到的結果沒有意義,這也是導致上面出現坑爹的結果。 [sql] mysql> select t1, t2, convert(t1, UNSIGNED INTEGER) ct1, convert(t2, UNSIGNED INTEGER) ct2, t2-t1, convert(t2, UNSIGNED INTEGER) -convert(t1, UNSIGNED INTEGER) diff0 from mytest; +-------------------+-------------------+--------------+--------------+-----+-----+ |t1 |t2 |ct1 |ct2 |t2-t1|diff0| +-------------------+-------------------+--------------+--------------+-----+-----+ |2013-04-21 16:59:33|2013-04-21 16:59:43|20130421165933|20130421165943| 10| 10| |2013-04-21 16:59:33|2013-04-21 17:00:33|20130421165933|20130421170033| 4100| 4100| |2013-04-21 16:59:33|2013-04-21 17:59:35|20130421165933|20130421175935|10002|10002| +-------------------+-------------------+--------------+--------------+-----+-----+ 3 rows in set 要得到正確的時間相減秒值,有以下3種方法: 1、time_to_sec(timediff(t2, t1)), 2、timestampdiff(second, t1, t2), 3、unix_timestamp(t2) -unix_timestamp(t1) [sql] --測試腳本 mysql> select t1, t2, t2-t1, time_to_sec(timediff(t2, t1)) diff1, timestampdiff(second, t1, t2) diff2, unix_timestamp(t2) -unix_timestamp(t1) diff3 from mytest; +---------------------+---------------------+-------+-------+-------+-------+ | t1 | t2 | t2-t1 | diff1 | diff2 | diff3 | +---------------------+---------------------+-------+-------+-------+-------+ | 2013-04-21 16:59:33 | 2013-04-21 16:59:43 | 10 | 10 | 10 | 10 | | 2013-04-21 16:59:33 | 2013-04-21 17:00:33 | 4100 | 60 | 60 | 60 | | 2013-04-21 16:59:33 | 2013-04-21 17:59:35 | 10002 | 3602 | 3602 | 3602 | +---------------------+---------------------+-------+-------+-------+-------+ 3 rows in set 這個問題2003年就有人在mysql4.0的版本時反饋,但mysql官方並不認為是bug,因為他們認為mysql並不支持時間直接相減操作,應該用專用函數處理,所以一直沒有修正。但我認為這個很容易導致使用錯誤,要麼就直接報錯,要麼顯示正確的結果。