2013-05-26 60 views

回答

2

所有投射或修改字段timer的解決方案都不會讓索引工作,這意味着這些查詢會很慢。相同@Stevens答案我會寫:

UPDATE records 
    SET firstname="bob" 
WHERE lastname="jones" 
    AND timer >= cast(curdate() as datetime) 
    AND timer < cast(curdate() + interval 1 day as datetime); 

然後,你有正確的每一天。

編輯:錯誤是

AND timer => cast(curdate() as datetime) 

校正被

AND timer >= cast(curdate() as datetime) 
2

這將是您的最佳表現選項(計算日期範圍外部和使用BETWEEN)。投射你桌子的每一行將會顯着變慢。

UPDATE records SET firstname="bob" 
WHERE lastname="jones" 
    AND timer BETWEEN '2013-05-26' AND '2013-05-27' 
+0

高效,但它也會更新行'2013-05-27 00:00:00'' –

+0

是的,有時數據導入將帶來整個一天,就好像它發生在midnite衝程 – Drew

+0

@ypercube - 總是可以使用'BETWEEN'2013-05-26'AND'2013-05-26 23:59:59'' - 取決於是否需要1秒的精度... –

0

可以轉換爲DATE落時間部分,然後比較眼前這個。

UPDATE records SET firstname="bob" WHERE lastname="jones" AND CAST(timer AS DATE) = CAST(NOW() AS DATE) 
0

我會

where date(timer) = date(now()) 

注意,很可能會做奇怪的事情,如果你有改變時區,在這種情況下,我會改變一切成時代,即

where unix_timestamp(...pseudo code to get the proper midnight) <= unix_timestamp(timer) 
相關問題