在MySQL中如何存储时间值作为时间数据类型?

时间:2022-01-16 16:55:17

I need to store song durations in minutes/seconds, I need to use TIME, but how would I refer to a certain duration when I am writing an INSERT statement? My datatype in the table is already TIME, should I just STR_TO_DATE the string value of "4:29"?

我需要以分钟/秒为单位存储歌曲时长,我需要使用TIME,但是当我写插入语句时,我如何引用特定的时长呢?表中的数据类型已经是TIME,我是否应该只是STR_TO_DATE“4:29”的字符串值?

1 个解决方案

#1


6  

First, take a look here: http://dev.mysql.com/doc/refman/5.0/en/time.html

首先,看看这里:http://dev.sqmyl.com/doc/refman/5.0/en/time.html

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'.

在为时间列分配缩写值时要小心。MySQL解释缩写的时间值与冒号为时间。也就是说,“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”。

So, if you wanna insert a song with duration = 05:55, just write this:

所以,如果你想插入一首时长= 05:55的歌曲,你可以这样写:

insert into songs (duration) values('0555');

#1


6  

First, take a look here: http://dev.mysql.com/doc/refman/5.0/en/time.html

首先,看看这里:http://dev.sqmyl.com/doc/refman/5.0/en/time.html

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'.

在为时间列分配缩写值时要小心。MySQL解释缩写的时间值与冒号为时间。也就是说,“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”。

So, if you wanna insert a song with duration = 05:55, just write this:

所以,如果你想插入一首时长= 05:55的歌曲,你可以这样写:

insert into songs (duration) values('0555');