8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

13.2.3 TIME 类型13.2.3 The TIME Type

MySQL 以 'hh:mm:ss' 格式(对于较大的小时值,则为 'hhh:mm:ss' 格式)检索和显示 TIME 值。TIME 值的范围可以从 '-838:59:59' 到 '838:59:59'。小时部分可能非常大,因为 TIME 类型不仅可以用来表示一天中的时间(必须少于 24 小时),还可以表示经过的时间或两个事件之间的时间间隔(可能远大于 24 小时,甚至可能为负值)。MySQL retrieves and displays TIME values in 'hh:mm:ss' format (or 'hhh:mm:ss' format for large hours values). TIME values may range from '-838:59:59' to '838:59:59'. The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).

MySQL 识别多种格式的 TIME 值,其中一些格式可以包含最多微秒(6 位)精度的尾随小数秒部分。请参阅 第 11.1.3 节,“日期和时间字面量”。有关 MySQL 中小数秒支持的信息,请参阅 第 13.2.6 节,“时间值中的小数秒”。特别是,插入到 TIME 列的值中的任何小数部分都会被存储,而不是丢弃。包括小数部分时,TIME 值的范围是 '-838:59:59.000000' 到 '838:59:59.000000'。MySQL recognizes TIME values in several formats, some of which can include a trailing fractional seconds part in up to microseconds (6 digits) precision. See Section 11.1.3, “Date and Time Literals”. For information about fractional seconds support in MySQL, see Section 13.2.6, “Fractional Seconds in Time Values”. In particular, any fractional part in a value inserted into a TIME column is stored rather than discarded. With the fractional part included, the range for TIME values is '-838:59:59.000000' to '838:59:59.000000'.

在给 TIME 列赋值缩写值时要小心。MySQL 将带冒号的缩写 TIME 值解释为一天中的时间。也就是说,'11:12' 表示 '11:12:00',而不是 '00:11:12'。MySQL 将不带冒号的缩写值解释为其右边两位数字表示秒数(即作为经过的时间而非一天中的时间)。例如,你可能认为 '1112' 和 1112 表示 '11:12:00'(11 点 12 分),但 MySQL 将它们解释为 '00:11:12'(11 分 12 秒)。同样,'12' 和 12 被解释为 '00:00:12'。Be careful about assigning abbreviated values to a TIME column. MySQL interprets abbreviated TIME values with colons as time of the day. That is, '11:12' means '11:12:00', not '00:11:12'. MySQL interprets abbreviated values without colons using the assumption that the two rightmost digits represent seconds (that is, as elapsed time rather than as time of day). For example, you might think of '1112' and 1112 as meaning '11:12:00' (12 minutes after 11 o'clock), but MySQL interprets them as '00:11:12' (11 minutes, 12 seconds). Similarly, '12' and 12 are interpreted as '00:00:12'.

时间部分和秒的小数部分之间唯一被认可的分隔符是小数点。The only delimiter recognized between a time part and a fractional seconds part is the decimal point.

默认情况下,超出 TIME 范围但 otherwise 有效的值会被裁剪到该范围的最接近端点。例如,'-850:00:00' 和 '850:00:00' 会被转换为 '-838:59:59' 和 '838:59:59'。无效的 TIME 值会被转换为 '00:00:00'。请注意,由于 '00:00:00' 本身是一个有效的 TIME 值,因此无法从表中存储的 '00:00:00' 值判断原始值是指定为 '00:00:00' 还是无效值。By default, values that lie outside the TIME range but are otherwise valid are clipped to the closest endpoint of the range. For example, '-850:00:00' and '850:00:00' are converted to '-838:59:59' and '838:59:59'. Invalid TIME values are converted to '00:00:00'. Note that because '00:00:00' is itself a valid TIME value, there is no way to tell, from a value of '00:00:00' stored in a table, whether the original value was specified as '00:00:00' or whether it was invalid.

为了对无效的 TIME 值进行更严格的处理,可以启用严格 SQL 模式以导致错误发生。请参阅 第 7.1.11 节,“服务器 SQL 模式”。For more restrictive treatment of invalid TIME values, enable strict SQL mode to cause errors to occur. See Section 7.1.11, “Server SQL Modes”.