简体   繁体   English

SQL 如何在 SQL 查询中将毫秒从表转换为 HH:MM:SS?

[英]SQL How to convert Milliseconds from a table to HH:MM:SS in SQL Query?

PHP version: 7.3.6 using phpMyAdmin PHP 版本:7.3.6 使用 phpMyAdmin

I have seen a few posts about this but I've struggled to find an answer that works for me.我看过一些关于此的帖子,但我一直在努力寻找适合我的答案。 I am trying to get the race result time to display in HH:MM:SS which is currently in milliseconds in the table我试图让比赛结果时间显示在 HH:MM:SS 中,目前在表中以毫秒为单位

SELECT
MIN(Result.Time) AS MinTime, event_cat_id

FROM
Wp_wpa_result Result
INNER JOIN Wp_wpa_event Race ON Result.Event_id = Race.Id 
WHERE Race.Sub_type_id = 'R' AND Age_grade <> 'null' AND event_cat_id IN ( '30', '11', '12', '13', '14', '17', '20', '19', '15'  )
GROUP BY event_cat_id
)


SELECT  U.Display_name, EVC.Name AS EventDistance, EV.name AS RaceName, MinTime,  EV.Date, RE.Age_Grade AS AgeGrade, EV.Sub_type_id AS Terrain, EV.Date AS RaceDate
FROM CTE

INNER JOIN Wp_wpa_event_cat EVC ON EVC.id = CTE.event_cat_id  
INNER JOIN Wp_wpa_event EV ON EV.Event_cat_id = CTE.event_cat_id 
INNER JOIN Wp_wpa_result RE ON EV.id = RE.event_id AND CTE.MinTime = RE.Time
INNER JOIN wp_users U ON U.ID = RE.user_id
ORDER BY MinTime;

Display_name ||显示名称 || EventDistance ||事件距离 || RaceName ||种族名 || MinTime ||最小时间 || Date ||日期 || AgeGrade ||年龄等级 || Terrain ||地形 || RaceDate比赛日期

Dave Blogs ||戴夫博客 || 5k Worden Place 5k || 5k 沃登广场 5k || 1042000 || 1042000 || 06/04/2019 || 2019 年 6 月 4 日 || 74.76 || 74.76 || R || R || 06/04/2019 2019 年 6 月 4 日

But should be但应该是

Dave Blogs ||戴夫博客 || 5k Worden Place 5k || 5k 沃登广场 5k || 00:17:14 || 00:17:14 || 06/04/2019 || 2019 年 6 月 4 日 || 74.76 || 74.76 || R || R || 06/04/2019 2019 年 6 月 4 日

Thanks, Adam谢谢,亚当

You need the function sec_to_time() :您需要 function sec_to_time()

sec_to_time(MinTime / 1000)

If you want the result formatted as hh:mm:ss then use also time_format() :如果您希望将结果格式化为hh:mm:ss然后使用time_format()

time_format(sec_to_time(MinTime / 1000), '%H %i %s') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM