MySQL:将datetime插入其他datetime字段 [英] MySQL: Insert datetime into other datetime field

查看:179
本文介绍了MySQL:将datetime插入其他datetime字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来很简单,但我不确定如何做到最好,Google不会帮助。

This sounds so easy, but I am uncertain how to do it best, and Google wouldn't help.

我有一个带有DATETIME列的表。
我想选择这个datetime值并将其插入另一列。

I have a table with a DATETIME column. I would like to SELECT this datetime value and INSERT it into another column.

我这样做了(注:'2011-12-18 13:17: 17'是前一个SELECT从DATETIME字段提供的值):

I did this (note: '2011-12-18 13:17:17' is the value the former SELECT gave me from the DATETIME field):

UPDATE products SET former_date=2011-12-18 13:17:17 WHERE id=1

并获得

    1064 - You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version 
for the right syntax to use near '13:17:17 WHERE itemid=1' at line 1


我放在哪里做什么?
我想要的是将现有值可靠地传递到新的datetime字段...

Ok, I understand it's wrong to put an unquoted string in there, but is DATETIME just a string in the first place? What do I put in there? All I want is reliably transfer the existing value over to a new datetime field...

感谢您阅读!

编辑:

对不起,如果这是一个愚蠢的问题,我问的原因是:我有这个特殊的定义, DATETIME,不知何故,我认为在处理日期时给我一些安全性和其他优势。现在看来,它只是一个专门的VARCHAR,可以这么说。

Sorry if this is a stupid questions, the reason I ask is: I have this special definition, DATETIME, and somehow I thought it gives me some security and other advantages when handling dates. Now it seems it is simply a specialized VARCHAR, so to speak.

感谢您的回答,似乎这确实是预期的行为。

Thanks for your answers, it seems this is indeed the intended behaviour.

推荐答案

根据MySQL文档,您应该可以将该datetime字符串包含在单引号中('YYYY-MM- DD HH:MM:SS'),它应该工作。看这里:
日期和时间文字

According to MySQL documentation, you should be able to just enclose that datetime string in single quotes, ('YYYY-MM-DD HH:MM:SS') and it should work. Look here: Date and Time Literals

这篇关于MySQL:将datetime插入其他datetime字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆