Difference in minutes from two time fields in MySQL

后端 未结 4 1221
执笔经年
执笔经年 2020-11-29 09:47

I set up my MySQL database with the field \'time\'

It is not HH:MM in the traditional sense, it is the time an event occurred, so an event with the value of 5:45 occ

4条回答
  •  情话喂你
    2020-11-29 10:44

    I needed similar. Two useful functions: TIME_TO_SEC and SUBTIME.

    e.g. if your time fields were normal HH:MM times, then

    SELECT (TIME_TO_SEC(end_time) - TIME_TO_SEC(start_time))/60 AS `minutes` 
    

    In your case, as I understand it, the times are backwards, ie. 12:00<6:00 so your end_time and start_time would need swapping.

    If you wanted the output in HH:MM:SS you could do

    SELECT SUBTIME(end_time, start_time)
    

提交回复
热议问题