开发者

MySQL query to normalise datetime field

I have an application that used to insert a datetime field. There was an issue though that it would insert with the seconds and it wasn't supposed to!

I.e. 2011-08-07 15:24:06

They have corrected this and inserts now only include up to the minute so the above example would look like

2011-08-开发者_如何转开发07 15:24:00

There are 20 million rows "wrong" at the moment. What is the most efficient query to fix the old ones?


UPDATE
  table
SET
  timefield = DATE_FORMAT(timefield,'%Y-%m-%d %H:%i:00')
WHERE
  SECOND(timefield) <> 0;

This will have to read each row of the table and extract the seconds part of the time (this is unavoidable), but it won't have to update rows which already are correct.


Probably filter your update based on the 'wrong' ones then do a right trim of the last 3 characters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜