UPDATE blogs SET start_date = '11/27/2012 00:00',end_date = '11/27/2012 00:00' WHERE id='9'
此查询不会存储博客 ID 9
的 start_date
或 end_date
值,除非我将它们设置为 varchar
类型。
尝试使用时间戳
和日期时间
,但查询始终会返回受影响的 0 行
问题是我需要能够在一段时间内检查行,而使用 varchar 它会变得非常复杂。
我错过了什么?
最佳答案
如 Date and Time Literals 中所述:
MySQL recognizes
DATETIME
andTIMESTAMP
values in these formats:
As a string in either
'YYYY-MM-DD HH:MM:SS'
or'YY-MM-DD HH:MM:SS'
format. A “relaxed” syntax is permitted here, too: Any punctuation character may be used as the delimiter between date parts or time parts. For example,'2012-12-31 11:30:45'
,'2012^12^31 11+30+45'
,'2012/12/31 11*30*45'
, and'2012@12@31 11^30^45'
are equivalent.As a string with no delimiters in either
'YYYYMMDDHHMMSS'
or'YYMMDDHHMMSS'
format, provided that the string makes sense as a date. For example,'20070523091528'
and'070523091528'
are interpreted as'2007-05-23 09:15:28'
, but'071122129015'
is illegal (it has a nonsensical minute part) and becomes'0000-00-00 00:00:00'
.As a number in either
YYYYMMDDHHMMSS
orYYMMDDHHMMSS
format, provided that the number makes sense as a date. For example,19830905132800
and830905132800
are interpreted as'1983-09-05 13:28:00'
.
因此,字符串 '11/27/2012 00:00'
和 '11/27/2012 00:00'
不是有效的 MySQL 日期时间文字。您有两个选择(按照一些模糊的偏好顺序,没有任何有关您的要求的进一步信息):
以可识别的格式提供您的文字:
UPDATE blogs SET start_date = '2012-11-27 00:00:00', end_date = '2012-11-27 00:00:00' WHERE id = 9
使用 MySQL 的
STR_TO_DATE()
转换字符串的函数:UPDATE blogs SET start_date = STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %H:%i'), end_date = STR_TO_DATE('11/27/2012 00:00', '%m/%d/%Y %H:%i') WHERE id = 9
关于mysql - 无法存储输入值的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13366685/