简体   繁体   English

MySQL从时间戳字段创建日期时间字段

[英]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. 我们正在将新闻系统从CuteNews移至我们自己构建的MySQL系统。 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. 我创建了一个新字段(created_date),并希望从date字段的Timestamp中填充它。

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) . 就像我说的那样,我以前是在早期的试用版中这样做的,并且确信我通过PhpMyAdmin使用的查询符合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. 没有返回错误,但是所有“ created_date”字段都填充为0000-00-00 00:00而不是在date字段中使用时间戳记中的日期。

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. 在我使用更新查询的原始试运行中,基于相应的时间戳字段将正确的DateTime放入字段中。 It is only this time that it shows 0000-00-00 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. 不是UNIX_TIMESTAMP而是我FROM_UNIXTIME

UPDATE news SET created_date = FROM_UNIXTIME(date)

Thanks for the help. 谢谢您的帮助。

Steve 史蒂夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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