代码之家  ›  专栏  ›  技术社区  ›  Straff

mysql now()列的日期时间值不正确-错误代码1292

  •  2
  • Straff  · 技术社区  · 15 年前

    使用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)
    

    有时,即使被期望,也不会给出选择结果。

    2 回复  |  直到 15 年前
        1
  •  4
  •   Straff    15 年前

    用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 );
    

    但问题的间歇性本质是“伤害”。

        2
  •  1
  •   T.J. Crowder    15 年前

    我建议使用 DateAdd 相反,为了可靠性和可读性。