无论使用哪一种RDBMS,都需要使用到其中的一些日期转换函数,在使用MySQL的AddDate函数时,遇到了点小问题,稍作记录。

root@localhost:mysql3376.sock  [(none)]>select now() , addtime(now(),'33 0:0:1') ; 
+---------------------+---------------------------+
| now()               | addtime(now(),'33 0:0:1') |
+---------------------+---------------------------+
| 2017-02-08 11:23:50 | 2017-03-13 11:23:51       |
+---------------------+---------------------------+
1 row in set (0.00 sec)

root@localhost:mysql3376.sock  [(none)]>select now() , addtime(now(),'34 0:0:1') ; 
+---------------------+---------------------------+
| now()               | addtime(now(),'34 0:0:1') |
+---------------------+---------------------------+
| 2017-02-08 11:23:54 | 2017-03-14 11:23:55       |
+---------------------+---------------------------+
1 row in set (0.00 sec)

root@localhost:mysql3376.sock  [(none)]>select now() , addtime(now(),'35 0:0:1') ; 
+---------------------+---------------------------+
| now()               | addtime(now(),'35 0:0:1') |
+---------------------+---------------------------+
| 2017-02-08 11:24:01 | 2017-03-15 10:24:00       |
+---------------------+---------------------------+
1 row in set, 1 warning (0.00 sec)

root@localhost:mysql3376.sock  [(none)]>select now() , addtime(now(),'36 0:0:1') ; 
+---------------------+---------------------------+
| now()               | addtime(now(),'36 0:0:1') |
+---------------------+---------------------------+
| 2017-02-08 11:24:05 | 2017-03-15 10:24:04       |
+---------------------+---------------------------+
1 row in set, 1 warning (0.00 sec)

  仔细一看上文的4条SQL语句,第一句和第二句的结果是结果是正确的,但第三句和第四句的结果并不是期望的结果。

  再仔细一看,第三句和第四句相比于第一句和第二句多输出了一个“1 warning”。来看看warning是提示了什么

root@localhost:mysql3376.sock  [(none)]>show warnings ;
+---------+------+--------------------------------------------+
| Level   | Code | Message                                    |
+---------+------+--------------------------------------------+
| Warning | 1292 | Truncated incorrect time value: '36 0:0:1' |
+---------+------+--------------------------------------------+
1 row in set (0.00 sec)

  warning提示“截断不正确的时间值”。为什么这个时间值不正确呢? 

  MySQL官方文档中有说明time值格式的取值说明:

MySQL recognizes TIME values in these formats: 

• As a string in 'D HH:MM:SS' format. You can also use one of the following “relaxed”syntaxes: 'HH:MM:SS', 'HH:MM', 'D HH:MM', 'D HH', or 'SS'. Here D represents days and can have a value from 0 to 34. 

• As a string with no delimiters in 'HHMMSS' format, provided that it makes sense as a time. For example, '101112' is understood as '10:11:12', but '109712' is illegal (it has a nonsensical minute part) and becomes '00:00:00'.
 
• As a number in HHMMSS format, provided that it makes sense as a time. For example, 101112 is understood as '10:11:12'. The following alternative formats are also understood: SS, MMSS, or HHMMSS.
 

  总结: 

    一、虽然MySQL的官方文档说明了time value中“D”的值只能是从0~34,但没有具体说明原因; 

    二、遇到问题,需要仔细查看输出内容,我只是留意到了结果有误,但并没有注意到有warning,还好有高人指点; 

 

  以上,如有错谬,请不吝指正。

posted on 2017-02-08 14:38  止獨  阅读(486)  评论(0编辑  收藏  举报