MySQL 和日期时间

标签 mysql datetime

如果我有一个包含 DATETIME 列的表,我可以插入格式如下的日期:

2015-03-25 10:10:10
2015-03-25 10:10
2015-03-25 10
2015-03-25 

它将用零填充余数。但我不能使用

2015-03 
2015

因为它会给出“日期时间值不正确”错误。然而,可以在像 [..] WHERE timestamp < '2015-03' 这样的 SELECT 中使用最后两个。 ..

如果在日期时间中省略,MySQL 是否可以用 01-01 来填充日期时间的其余部分,或者我必须自己手动执行此操作?

即我想在 INSERT 语句中使用“2015-03”,或者执行类似 SELECT DATE_FORMAT('2015-03', '%Y%m%dT%H%i%S') 的操作

最佳答案

Date and Time Literals 中所述:

MySQL recognizes DATE values in these formats:

  • As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A “relaxed” syntax is permitted: Any punctuation character may be used as the delimiter between date parts. For example, '2012-12-31', '2012/12/31', '2012^12^31', and '2012@12@31' are equivalent.

  • As a string with no delimiters in either 'YYYYMMDD' or 'YYMMDD' format, provided that the string makes sense as a date. For example, '20070523' and '070523' are interpreted as '2007-05-23', but '071332' is illegal (it has nonsensical month and day parts) and becomes '0000-00-00'.

  • As a number in either YYYYMMDD or YYMMDD format, provided that the number makes sense as a date. For example, 19830905 and 830905 are interpreted as '1983-09-05'.

MySQL recognizes DATETIME and TIMESTAMP 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.

    The only delimiter recognized between a date and time part and a fractional seconds part is the decimal point.

    The date and time parts can be separated by T rather than a space. For example, '2012-12-31 11:30:45' '2012-12-31T11: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 or YYMMDDHHMMSS format, provided that the number makes sense as a date. For example, 19830905132800 and 830905132800 are interpreted as '1983-09-05 13:28:00'.

值得注意的是,MySQL 不支持您希望使用的不完整格式。

MySQL碰巧接受您尝试过的一些不完整格式(显然是通过用零填充)是未记录的行为,很可能无意的开发人员。。它不能(并且不应该)被依赖,尤其是因为可能存在导致行为中断的边缘情况;或者因为在未来的版本中该行为可能会在没有警告的情况下发生更改。

如果绝对有必要向MySQL提供这种不完整的时间文字(其实不应该这样,因为你的数据访问层应该知道它正在处理的值的类型并提供它们)以支持的格式复制到 MySQL),您可以使用其 STR_TO_DATE()函数来相应地解析它们:

Unspecified date or time parts have a value of 0, so incompletely specified values in str produce a result with some or all parts set to 0:

mysql> <strong>SELECT STR_TO_DATE('abc','abc');</strong>
        -> '0000-00-00'
mysql> <strong>SELECT STR_TO_DATE('9','%m');</strong>
        -> '0000-09-00'
mysql> <strong>SELECT STR_TO_DATE('9','%s');</strong>
        -> '00:00:09'

Range checking on the parts of date values is as described in Section 11.3.1, “The DATE, DATETIME, and TIMESTAMP Types”. This means, for example, that “zero” dates or dates with part values of 0 are permitted unless the SQL mode is set to disallow such values.

例如,您可以使用:

STR_TO_DATE('2015-03', '%Y-%m');

关于MySQL 和日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29253679/

相关文章:

javascript - 将 Java OffsetDateTime 解析为 JS 日期

c# - 为什么 DateTime 不是预定义类型?

r - 在没有时区的 R 中创建日期和时间序列

mysql - 在mysql数据库中创建一个默认值为null的列

java - 我需要在 Postgres 中格式化日期

php - MySQL 错误代码 : 1305. FUNCTION JSON_EXTRACT 在 MySQL 客户端版本 : 5. 5.52 中不存在

Mysql docker 容器 - 无法让任何东西工作

MySQL获取两个日期值之间所有天数的最佳方法

php - 在 Laravel 中同时执行多个查询

php - 使用 PHP + AJAX 同时从两个 MySQL 表中获取数据