How to store time duration values ​​in MySQL as TIME datatype?

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

+3


source to share


1 answer


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

Be careful when assigning truncated values ​​to the TIME column. MySQL interprets abbreviated TIME values ​​with colons as the time of day. That is, "11:12" means "11:12: 00", not "00: 11:12". MySQL interprets truncated values ​​without colons, assuming that the two right-most digits represent seconds (that is, as elapsed time rather than time of day). For example, you might think of "1112" and 1112 as "11:12:00" (12 minutes after 11:00), but MySQL interprets them as "00: 11:12" (11 minutes, 12 seconds). By analogy, "12" and "12" are interpreted as "00: 00: 12".



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

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

      

+6


source







All Articles