错误测试mysql时间相减的问题(bug)
在写这篇文章之前,xxx已经写过了几篇关于改错误测试主题的文章,想要了解的朋友可以去翻一下之前的文章
mysql时光相减的问题(bug)
明天看到宁青同窗的一条微博,提到mysql日期相减的错误结果,之前没有怎么意注,于是测试了一下,发明实确很坑爹,很易容踩雷,于是理整客博提示一下大家。
先看一下错误的景象如下,第1条确正,第2,3条的t2-t1不确正:
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> 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,由于时光不是十进制,所以最后到得的结果没有意义,这也是致使下面现出坑爹的结果。
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)
--测试本脚 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其实不支撑时光直接相减作操,应该用专用函数理处,所以直一没有修正。但我以为这个很易容致使用使错误,要么就直接报错,要么示显确正的结果。
我的新浪微博 http://weibo.com/yzsind
文章结束给大家分享下程序员的一些笑话语录:
问:你觉得让你女朋友(或者任何一个女的)从你和李彦宏之间选一个,你觉得她会选谁?
答:因为李艳红这种败类,所以我没女友!