I'm currently storing all DATETIME
fields in my database in US Eastern time, and would like to UPDATE
them all to UTC. I have a lot of DATETIME
fields in lots of different tables. Is there a way to identify and UPDATE
them all at once?
You could generate the UPDATE statements that you need with following statements:
SET @tzdiff = 5; -- difference between EST and UTC
SET @db = 'test4'; -- your DB name
SELECT
CONCAT(
'UPDATE ',
'`', c.TABLE_NAME, '` ',
'SET ',
'`', COLUMN_NAME, '` = ',
'`', COLUMN_NAME, '` + INTERVAL ', @tzdiff, ' HOUR;'
) as update_statement
FROM
INFORMATION_SCHEMA.COLUMNS c
WHERE
c.TABLE_SCHEMA = @db
AND
c.`DATA_TYPE` LIKE 'datetime';
Result for my database 'test4':
update_statement
-----------------------------------------------------------------------
UPDATE `example12` SET `column_name` = `column_name` + INTERVAL 5 HOUR;
UPDATE `post` SET `expiration` = `expiration` + INTERVAL 5 HOUR;
For a one-time-job, I would simply copy the result into the MySQL client of your choice and execute them. Of course it's possible to create prepared statements for the results and execute them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.