2014-04-25 19 views
0

請幫我在SQL查詢中第二次轉換爲時間。 我正在使用MySql函數SEC_TO_TIME。 「:59:59 838」,我有1000小時以上如何將第二次轉換爲使用MYSQL的時間

TIME_FORMAT(SEC_TO_TIME(COALESCE(ROUND(NBSECONDS), 0)),'%H:%i:%s') AS MYTIME 

,因爲它限於我必須找到這個功能的替代品。

謝謝

+0

檢查這個問題的http://計算器。 COM /問題/ 13733148 /超越-mysqls時間價值漲停的-8385959 –

回答

1

你可以簡單地寫在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;[email protected](playground)> select my_sec_to_time(3020399); 
+-------------------------+ 
| my_sec_to_time(3020399) | 
+-------------------------+ 
| 838:59:59    | 
+-------------------------+ 
1 row in set (0.00 sec) 

mysql;[email protected](playground)> select my_sec_to_time(3020400); 
+-------------------------+ 
| my_sec_to_time(3020400) | 
+-------------------------+ 
| 839:0:0     | 
+-------------------------+ 
1 row in set (0.00 sec) 

mysql;[email protected](playground)> select my_sec_to_time(4020400); 
+-------------------------+ 
| my_sec_to_time(4020400) | 
+-------------------------+ 
| 1116:46:40    | 
+-------------------------+ 
1 row in set (0.00 sec) 
1
SELECT @t; 
+---------+ 
| @t  | 
+---------+ 
| 3603750 | 
+---------+ 

SELECT CONCAT_WS(':' 
       ,LPAD(FLOOR(@t/3600),2,0) 
       ,LPAD(ROUND(FLOOR((@t/3600-FLOOR(@t/3600))*60)),2,'0') 
       ,LPAD(ROUND((((@t/3600-FLOOR(@t/3600))*60) - FLOOR((@t/3600-FLOOR(@t/3600))*60)) * 60),2,0) 
       )x; 

+------------+ 
| x   | 
+------------+ 
| 1001:02:30 | 
+------------+ 
相關問題