MySQLのDATE/DATETIME型には、利用しないほうが良い「0000-00-00 00:00:00」という値がある。 詳細は他の方に譲るとして、今回はこのあたりをUPDATEしたいと思う。
mysql> UPDATE buildings set created_at = '2004-01-29 00:00:00' where created_at = '0000-00-00 00:00:00'; ERROR 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00' for column 'created_at' at row 1
エラー出て更新できない。
mysql> select id from buildings where created_at = '0000-00-00 00:00:00';
created_atが '0000-00-00 00:00:00'になっているデータがヒットする。
mysql> UPDATE buildings set created_at = '2004-01-29 00:00:00' where id in (select id from buildings where created_at = '0000-00-00 00:00:00'); ERROR 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00' for column 'created_at' at row 1
サブクエリでヒットさせようとしたけど失敗。
UPDATE buildings set created_at = '2004-01-29 00:00:00' where CAST(created_at AS CHAR(20)) = '0000-00-00 00:00:00';
StackOverFlowで検索したらこれならできるらしい。めんどくさ。