首页 > 解决方案 > MySQL data to time value

问题描述

My table does an addition of how much time it takes to finish production.

It spits out values like: 192.5, or 100.25.

I'd like to know how to turn these values into something like this:

120 ---> 120 minutes

140.5 ---> 2 hours, 20 minutes, 30 seconds

30.25 ---> 30 mintes, 15 seconds

The formatting is unimportant, just would like to display time.

标签: mysqlmydbr

解决方案


Crudely (I suspect that this can be written more elegantly)...

SET @x = 30.25;

SELECT FLOOR(@x/60) hours
     , FLOOR((@x/ 60-FLOOR(@x/60)) * 60) minutes
     , ROUND(((((@x/60)-FLOOR(@x/60))*60)-FLOOR((@x/60-FLOOR(@x/60))*60))*60,0) seconds;
+-------+---------+---------+
| hours | minutes | seconds |
+-------+---------+---------+
|     0 |      30 |      15 |
+-------+---------+---------+

推荐阅读