2009-09-25 55 views
2

做時使用簡單的數學NOW()...MySQL的NOW()列不正確的時間值 - 錯誤代碼1292

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) 

,有時儘管預期會出現選擇結果。

回答

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

我建議使用DateAdd來代替可靠性和可讀性。

+0

好的,可能會這樣做,但我的問題是「簡單的數學」機制的不可靠性。如果它沒有實際用途,或者不能被依賴,那麼它確實應該被當作語法錯誤或其他東西拒絕。 – Straff 2009-10-12 03:26:18

+0

@Straff:完全同意。 – 2009-10-12 08:58:44

相關問題