mysql addtime()似乎不能工作很长时间(很多天)

时间:2014-11-11 17:40:21

标签: mysql

mysql> select * from data order by poll_time desc limit 10;
+-----------+---------------------+--------+-----------+----------------+
| server_id | poll_time           | oid_id | value     | value_interval |
+-----------+---------------------+--------+-----------+----------------+
|         2 | 2014-09-23 20:10:00 |    268 |         0 |              0 |
|         2 | 2014-09-23 20:10:00 |    267 |         0 |              0 |
|         2 | 2014-09-23 20:10:00 |    266 |        43 |              0 |
|         2 | 2014-09-23 20:10:00 |    265 |     21765 |              0 |
|         2 | 2014-09-23 20:10:00 |    263 |         0 |              0 |
|         2 | 2014-09-23 20:10:00 |    262 |   2560190 |              0 |
|         2 | 2014-09-23 20:10:00 |    261 | 204851561 |              0 |
|         2 | 2014-09-23 20:10:00 |    260 |  29941762 |              0 |
|         2 | 2014-09-23 20:10:00 |    258 |         0 |              0 |
|         2 | 2014-09-23 20:10:00 |    257 |         0 |              0 |
+-----------+---------------------+--------+-----------+----------------+
10 rows in set (0.00 sec)

mysql> UPDATE `data` 
       SET `poll_time` = ADDTIME(`poll_time`, '38 00:00:00') 
       ORDER BY `poll_time` DESC;

Query OK, 168700 rows affected, 65535 warnings (30.63 sec)
Rows matched: 168700  Changed: 168700  Warnings: 168700

mysql> select * from data order by poll_time desc limit 10;
+-----------+---------------------+--------+-----------+----------------+
| server_id | poll_time           | oid_id | value     | value_interval |
+-----------+---------------------+--------+-----------+----------------+
|         2 | 2014-10-28 19:09:59 |    268 |         0 |              0 |
|         2 | 2014-10-28 19:09:59 |    267 |         0 |              0 |
|         2 | 2014-10-28 19:09:59 |    266 |        43 |              0 |
|         2 | 2014-10-28 19:09:59 |    265 |     21765 |              0 |
|         2 | 2014-10-28 19:09:59 |    263 |         0 |              0 |
|         2 | 2014-10-28 19:09:59 |    262 |   2560190 |              0 |
|         2 | 2014-10-28 19:09:59 |    261 | 204851561 |              0 |
|         2 | 2014-10-28 19:09:59 |    260 |  29941762 |              0 |
|         2 | 2014-10-28 19:09:59 |    258 |         0 |              0 |
|         2 | 2014-10-28 19:09:59 |    257 |         0 |              0 |
+-----------+---------------------+--------+-----------+----------------+
10 rows in set (0.00 sec)

根据我的统计,38天应该带我们到10/31,而不是10/28。

我意识到这里有一个ADDDATE()函数,但是这是从一个脚本运行的,所以我希望有一个可以同时添加日期或时间的机制。

2 个答案:

答案 0 :(得分:2)

这是mysql中的一个错误。

BUG 25620BUG 11655 问题是时间值大于838:59:59,它会被截断为该值。 addtime将具有天数的时间转换为时间部分。在该时间内最多35天。

您应该使用date_add(),因为它只会更新日期部分并保留时间段相同的

SELECT 
  poll_time, 
  date_add(`poll_time`, INTERVAL 38 DAY) 
FROM data

FIDDLE DEMO

+----------------------------------+----------------------------------------+
|            POLL_TIME             | DATE_ADD(`POLL_TIME`, INTERVAL 38 DAY) |
+----------------------------------+----------------------------------------+
| September, 23 2014 20:10:00+0000 | October, 31 2014 20:10:00+0000         |
| September, 23 2014 20:10:00+0000 | October, 31 2014 20:10:00+0000         |
| September, 23 2014 20:10:00+0000 | October, 31 2014 20:10:00+0000         |
| September, 23 2014 20:10:00+0000 | October, 31 2014 20:10:00+0000         |
| September, 23 2014 20:10:00+0000 | October, 31 2014 20:10:00+0000         |
| September, 23 2014 20:10:00+0000 | October, 31 2014 20:10:00+0000         |
| September, 23 2014 20:10:00+0000 | October, 31 2014 20:10:00+0000         |
| September, 23 2014 20:10:00+0000 | October, 31 2014 20:10:00+0000         |
| September, 23 2014 20:10:00+0000 | October, 31 2014 20:10:00+0000         |
| September, 23 2014 20:10:00+0000 | October, 31 2014 20:10:00+0000         |
+----------------------------------+----------------------------------------+

编辑:

进一步研究看起来这是预期的行为。因为Mysql目前有大的限制。除此之外的任何事情都应该是日期计算。 DOCS

  

MySQL在' HH:MM:SS'中检索并显示TIME值。格式(或   ' HHH:MM:SS'大小时值的格式)。 TIME值的范围可以从   ' -838:59:59'到' 838:59:59'

答案 1 :(得分:0)

改为使用date_add()功能,您将获得所需的功能。

证明:

mysql> select date_add('2014-09-23 20:10:00', INTERVAL 38 DAY);
+--------------------------------------------------+
| date_add('2014-09-23 20:10:00', INTERVAL 38 DAY) |
+--------------------------------------------------+
| 2014-10-31 20:10:00                              |
+--------------------------------------------------+
1 row in set (0.00 sec)

mysql>