MySQL has fractional seconds support for MySQL支持TIME
, DATETIME
, and TIMESTAMP
values, with up to microseconds (6 digits) precision:TIME
、DATETIME
和TIMESTAMP
值的分秒支持,精度高达微秒(6位):
To define a column that includes a fractional seconds part, use the syntax 要定义包含分秒部分的列,请使用语法
, where type_name
(fsp
)type_name
is TIME
, DATETIME
, or TIMESTAMP
, and fsp
is the fractional seconds precision.
,其中type_name
(fsp
)type_name
是TIME
、DATETIME
或TIMESTAMP
,fsp
是分秒精度。For example:例如:
CREATE TABLE t1 (t TIME(3), dt DATETIME(6));
The fsp
value, if given, must be in the range 0 to 6. fsp
值(如果给定)必须在范围0到6之间。A value of 0 signifies that there is no fractional part. 值为0表示没有小数部分。If omitted, the default precision is 0. 如果省略,则默认精度为0。(This differs from the standard SQL default of 6, for compatibility with previous MySQL versions.)(为了与以前的MySQL版本兼容,这与标准SQL默认值6不同。)
Inserting a 在同一类型但小数位数较少的列中插入带有小数部分的TIME
, DATE
, or TIMESTAMP
value with a fractional seconds part into a column of the same type but having fewer fractional digits results in rounding. TIME
、DATE
或TIMESTAMP
值将导致舍入。Consider a table created and populated as follows:考虑如下所示创建和填充的表:
CREATE TABLE fractest( c1 TIME(2), c2 DATETIME(2), c3 TIMESTAMP(2) ); INSERT INTO fractest VALUES ('17:51:04.777', '2018-09-08 17:51:04.777', '2018-09-08 17:51:04.777');
The temporal values are inserted into the table with rounding:时间值以舍入方式插入表中:
mysql> SELECT * FROM fractest;
+-------------+------------------------+------------------------+
| c1 | c2 | c3 |
+-------------+------------------------+------------------------+
| 17:51:04.78 | 2018-09-08 17:51:04.78 | 2018-09-08 17:51:04.78 |
+-------------+------------------------+------------------------+
No warning or error is given when such rounding occurs. 当这种舍入发生时,不会给出警告或错误。This behavior follows the SQL standard.此行为遵循SQL标准。
To insert the values with truncation instead, enable the TIME_TRUNCATE_FRACTIONAL
SQL mode:
SET @@sql_mode = sys.list_add(@@sql_mode, 'TIME_TRUNCATE_FRACTIONAL');
With that SQL mode enabled, the temporal values are inserted with truncation:启用该SQL模式后,将使用截断插入时态值:
mysql> SELECT * FROM fractest;
+-------------+------------------------+------------------------+
| c1 | c2 | c3 |
+-------------+------------------------+------------------------+
| 17:51:04.77 | 2018-09-08 17:51:04.77 | 2018-09-08 17:51:04.77 |
+-------------+------------------------+------------------------+
Functions that take temporal arguments accept values with fractional seconds. 采用时态参数的函数接受带小数秒的值。Return values from temporal functions include fractional seconds as appropriate. 时间函数的返回值包括适当的小数秒。For example, 例如,不带参数的NOW()
with no argument returns the current date and time with no fractional part, but takes an optional argument from 0 to 6 to specify that the return value includes a fractional seconds part of that many digits.NOW()
返回不带小数部分的当前日期和时间,但使用从0到6的可选参数指定返回值包括许多数字中的小数部分秒。
Syntax for temporal literals produces temporal values: 时态文字的语法生成时态值:DATE '
, str
'TIME '
, and str
'TIMESTAMP '
, and the ODBC-syntax equivalents. str
'DATE '
、str
'TIME '
和str
'TIMESTAMP '
,以及ODBC语法等价物。str
'The resulting value includes a trailing fractional seconds part if specified. 如果指定,结果值包括尾部的小数部分秒。Previously, the temporal type keyword was ignored and these constructs produced the string value. 以前,时代类型关键字被忽略,这些构造生成字符串值。See Standard SQL and ODBC Date and Time Literals请参阅标准SQL和ODBC日期和时间文字。