简体   繁体   中英

MySQL create Datetime field from Timestamp field

I have a simple problem for which I found a solution in early testing but cannot replicate now that I need to do it for real.

We are moving our news system from CuteNews to our self-built MySQL system. All the news has been imported into the database but the date field is Timestamp. I have created a new field (created_date) and want to populate this from the Timestamp in the date field.

As I said, I did do this previously in an early trial run and was convinced that the query I used, via PhpMyAdmin was along the lines of UPDATE News set created_date=UNIX_TIMESTAMP(date) . This is probably slightly wrong as I am at work and posting from my phone but it was along those lines.

No errors were returned but all 'created_date' fields were populated as 0000-00-00 00:00 not with the date taken from the Timestamp in the date field.

I know it is simple and know the answer will be obvious when I see it but any pointers would be gratefully appreciated!

Steve.

EDIT: reading back through I realised a bit of what I posted may be misleading. In my original trial run using the update query put the correct DateTime in the field based on the corresponding Timestamp field. It is only this time that it shows 0000-00-00 00:00.

Ps. Thanks for the format tidy-up. It's a bit awkward on a phone!

I knew I was nearly there!! It was not UNIX_TIMESTAMP but FROM_UNIXTIME I was after.

UPDATE news SET created_date = FROM_UNIXTIME(date)

Thanks for the help.

Steve

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM