MySQL 对 TIME、DATETIME 和 TIMESTAMP 值支持分数秒,精度最高可达微秒(6 位数字):MySQL has fractional seconds support for
TIME,
DATETIME, and
TIMESTAMP values, with up to
microseconds (6 digits) precision:
要定义包含小数秒部分的列,请使用语法
,其中type_name(fsp)type_name是TIME、DATETIME或TIMESTAMP,而fsp是小数秒的精度。例如:To define a column that includes a fractional seconds part, use the syntax, wheretype_name(fsp)type_nameisTIME,DATETIME, orTIMESTAMP, andfspis the fractional seconds precision. For example:CREATE TABLE t1 (t TIME(3), dt DATETIME(6));
如果提供,
fsp的值必须在 0 到 6 之间。值为 0 表示没有小数部分。如果省略,默认精度为 0。(这与标准 SQL 的默认值 6 不同,以兼容以前的 MySQL 版本。)Thefspvalue, if given, must be in the range 0 to 6. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0. (This differs from the standard SQL default of 6, for compatibility with previous MySQL versions.)将带有小数秒部分的
TIME、DATE或TIMESTAMP值插入到具有相同类型但小数位较少的列中,会导致四舍五入。考虑如下创建和填充的表:Inserting aTIME,DATE, orTIMESTAMPvalue with a fractional seconds part into a column of the same type but having fewer fractional digits results in rounding. 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 | +-------------+------------------------+------------------------+在发生此类四舍五入时不会给出警告或错误。此行为遵循 SQL 标准。No warning or error is given when such rounding occurs. This behavior follows the SQL standard.
要改为插入带截断的值,请启用
TIME_TRUNCATE_FRACTIONALSQL 模式:To insert the values with truncation instead, enable theTIME_TRUNCATE_FRACTIONALSQL mode:SET @@sql_mode = sys.list_add(@@sql_mode, 'TIME_TRUNCATE_FRACTIONAL');
启用该 SQL 模式后,时间值将以截断的方式插入:With that SQL mode enabled, the temporal values are inserted with truncation:
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 | +-------------+------------------------+------------------------+接受时间参数的函数可以接受带有小数秒的值。时间函数的返回值会根据情况包含小数秒。例如,
NOW()不带参数时返回当前日期和时间,不包含小数部分,但可以带一个从 0 到 6 的可选参数,用于指定返回值包含指定位数的小数秒部分。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.时间字面量的语法会生成时间值:
DATE '、str'TIME '和str'TIMESTAMP 'str',以及 ODBC 语法的等效写法。如果指定了小数秒部分,生成的值将包括该部分。之前,时间类型关键字会被忽略,这些结构会生成字符串值。参见 标准 SQL 和 ODBC 日期与时间字面量Syntax for temporal literals produces temporal values:DATE ',str'TIME ', andstr'TIMESTAMP ', and the ODBC-syntax equivalents. 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 Literalsstr'