你能帮我在sql查询中将秒转换为时间吗?
我正在使用 MySql 函数 SEC_TO_TIME。
TIME_FORMAT(SEC_TO_TIME(COALESCE(ROUND(NBSECONDS), 0)),'%H:%i:%s') AS MYTIME
我必须找到此功能的替代方案,因为它仅限于“838:59:59”,而我的时间超过 1000 小时。
谢谢
你可以简单地在 MySQL 中编写自己的函数,如下所示:
drop function if exists my_sec_to_time;
delimiter $$
create function my_sec_to_time(p_sec int)
returns varchar(10)
deterministic
begin
declare v_hour int;
declare v_minute int;
declare v_tmp_sec int;
set v_hour = floor(p_sec / 3600);
set v_tmp_sec = p_sec - (v_hour * 3600);
set v_minute = floor(v_tmp_sec / 60);
set v_tmp_sec = v_tmp_sec - (v_minute * 60);
return concat(v_hour, ':', v_minute, ':', v_tmp_sec);
end $$
delimiter ;
行动中:
mysql;root@localhost(playground)> select my_sec_to_time(3020399);
+-------------------------+
| my_sec_to_time(3020399) |
+-------------------------+
| 838:59:59 |
+-------------------------+
1 row in set (0.00 sec)
mysql;root@localhost(playground)> select my_sec_to_time(3020400);
+-------------------------+
| my_sec_to_time(3020400) |
+-------------------------+
| 839:0:0 |
+-------------------------+
1 row in set (0.00 sec)
mysql;root@localhost(playground)> select my_sec_to_time(4020400);
+-------------------------+
| my_sec_to_time(4020400) |
+-------------------------+
| 1116:46:40 |
+-------------------------+
1 row in set (0.00 sec)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)