我在表格中有一个 float 字段小时,用于插入时间,如下所示:
If 6.3 means 6 hours and 30 minutes
If 8.04 means 8 hours and 4 minutes
现在我想将这些所有浮点值添加到另一个时间格式为new_hours的列
我需要添加以下条目:
If 6.3 in hours column then 06:30:00 in new_hours column
If 8.04 in hours column then 08:04:00 in new_hours column
我在小时列中有很多条目,我想通过一个单一的SQL查询从几小时添加new_hours。
任何帮助都会受到欢迎。谢谢。
答案 0 :(得分:1)
有很多方法;我用sec_to_time MySQL将一定时间的秒数转换为时间。
第一步是将你的浮动转换为秒(从午夜开始),然后将其包装到MariaDB [(none)]> select sec_to_time( cast(floor(8.04)*60*60 + ((8.04-floor(8.04))*100*60) as int) );
+-----------------------------------------------------------------------------+
| sec_to_time( cast(floor(8.04)*60*60 + ((8.04-floor(8.04))*100*60) as int) ) |
+-----------------------------------------------------------------------------+
| 08:04:00 |
+-----------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> select sec_to_time( cast(floor(6.3)*60*60 + ((6.3-floor(6.3))*100*60) as int) );
+--------------------------------------------------------------------------+
| sec_to_time( cast(floor(6.3)*60*60 + ((6.3-floor(6.3))*100*60) as int) ) |
+--------------------------------------------------------------------------+
| 06:30:00 |
+--------------------------------------------------------------------------+
1 row in set (0.00 sec)
:
.04
我非常确定从8.04
中提取update
的方法更简单...
update t set new_hours =
sec_to_time( cast(floor(hours)*60*60
+ ((hours-floor(hours))*100*60) as int) );
语句将是:
{{1}}
答案 1 :(得分:1)
您可以尝试以下查询
SELECT STR_TO_DATE(REPLACE(ROUND(YOUR_COL(i.e. 6.3),2), '.', ':'), '%h:%i %p')
或者你也可以试试这个
SELECT MAKETIME(LPAD(SUBSTRING_INDEX('6.3', '.', 1),2,0),RPAD(SUBSTRING_INDEX('6.3', '.', -1),2,0),00)