使用now()...
进行简单的数学运算时 mysql>
select cdrstatuschangets from cdrs where ( cdrstatuschangets < now() - 10 );
+---------------------+
| cdrstatuschangets |
+---------------------+
| 2009-09-25 13:55:50 |
+---------------------+
1 row in set (0.00 sec)
show warnings;
Empty set (0.00 sec)
它经常奏效,但有时候......
mysql>
select cdrstatuschangets from cdrs where ( cdrstatuschangets < now() - 50 );
+---------------------+
| cdrstatuschangets |
+---------------------+
| 2009-09-25 13:55:50 |
+---------------------+
1 row in set, 1 warning (0.00 sec)
show warnings;
+---------+------+-----------------------------------------------------------------------+
| Level | Code | Message | |
+---------+------+-----------------------------------------------------------------------+
| Warning | 1292 | Incorrect datetime value: '20090925211564.000000' for column 'cdrStatusChangeTS' at row 1 |
+---------+------+-----------------------------------------------------------------------+
1 row in set (0.00 sec)
尽管有预期,但有时候不会给出选择结果。
答案 0 :(得分:4)
使用now()进行简单的数学运算存在一个阴险的问题...秒和分钟的减法等基于一分钟100秒,一小时100分钟......
有时似乎有效,有时却没有。阴险。
mysql> select now(); select now() -10;
+---------------------+
| now() |
+---------------------+
| 2009-09-25 21:07:20 |
+---------------------+
1 row in set (0.00 sec)
+-----------------------+
| now() -10 |
+-----------------------+
| 20090925210710.000000 |
+-----------------------+
1 row in set (0.00 sec)
一切都很好,但是......
mysql> select now(); select now() -10;
+---------------------+
| now() |
+---------------------+
| 2009-09-25 21:08:02 |
+---------------------+
1 row in set (0.00 sec)
+-----------------------+
| now() -10 |
+-----------------------+
| 20090925210792.000000 |
+-----------------------+
1 row in set (0.00 sec)
显示92秒的时间戳(看起来像时间戳)。
原来我需要做更像
的事情select cdrstatuschangets from cdrs where ( cdrstatuschangets < now() - INTERVAL 50 SECOND );
但这是“伤害”问题的间歇性特征。
答案 1 :(得分:1)
我建议使用DateAdd
代替可靠性和可读性。