问题
I need to Update values in Mysql timestamp filed to a datetime value. for example for a field join date, currently the field type is int(11) and the values are in timestamp. I need change this fields as date time filed or create a new datetime filed , then populate the field with datetime value of old timestamp value.
It can do with php code with looping . If there is any mysql query for that it will be interesting.
Many thanks for all of your helps, Happy coding
回答1:
Alternatively, alter the table by adding another column of field DateTime. Name this column as DateTimeField or whatever you want. After the table has been altered, execute this query
UPDATE tableName
SET DateTimeField = FROM_UNIXTIME(yourOldTimstampField)
回答2:
1st add a new column:
ALTER TABLE yourTable
ADD COLUMN new_date DATETIME NOT NULL DEFAULT '2013-01-08 00:00:00' AFTER preceding_col
Then update:
UPDATE youTable SET new_date = FROM_UNIXTIME(old_date)
and after that you can drop the old column (if needed):
Alter TABLE yourTable drop column old_date;
来源:https://stackoverflow.com/questions/14208506/update-values-in-mysql-timestamp-to-a-datetime-value