Update values in Mysql timestamp to a datetime value.

巧了我就是萌 提交于 2019-12-22 22:07:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!