11.2.1 Date and Time Data Type Syntax日期和时间数据类型语法

The date and time data types for representing temporal values are DATE, TIME, DATETIME, TIMESTAMP, and YEAR.用于表示时间值的日期和时间数据类型是DATETIMEDATETIMETIMESTAMPYEAR

For the DATE and DATETIME range descriptions, supported means that although earlier values might work, there is no guarantee.对于DATETIMEDATE范围的描述,“支持”意味着尽管早期的值可能有效,但不能保证。

MySQL permits fractional seconds for TIME, DATETIME, and TIMESTAMP values, with up to microseconds (6 digits) precision. MySQL允许TIMEDATETIMETIMESTAMP值的小数秒,精度高达微秒(6位)。To define a column that includes a fractional seconds part, use the syntax type_name(fsp), where type_name is TIME, DATETIME, or TIMESTAMP, and fsp is the fractional seconds precision. 要定义包含分秒部分的列,请使用语法type_name(fsp),其中type_nameTIMEDATETIMETIMESTAMPfsp是分秒精度。For example:例如:

CREATE TABLE t1 (t TIME(3), dt DATETIME(6), ts TIMESTAMP(0));

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不同。)

Any TIMESTAMP or DATETIME column in a table can have automatic initialization and updating properties; see Section 11.2.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.表中的任何TIMESTAMPDATETIME列都可以具有自动初始化和更新属性;请参见第11.2.5节,“时间戳和日期时间的自动初始化和更新”

The SUM() and AVG() aggregate functions do not work with temporal values. SUM()AVG()聚合函数不能处理时态值。(They convert the values to numbers, losing everything after the first nonnumeric character.) (它们将值转换为数字,丢失第一个非数字字符之后的所有内容。)To work around this problem, convert to numeric units, perform the aggregate operation, and convert back to a temporal value. 要解决此问题,请转换为数字单位,执行聚合操作,然后转换回时间值。Examples:示例:

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time_col))) FROM tbl_name;
SELECT FROM_DAYS(SUM(TO_DAYS(date_col))) FROM tbl_name;