如何在 MySQL 中将时间格式化为“2:34 小时"、“0:34 小时"等 [英] How to format time to '2:34 hrs', '0:34 hrs' etc in MySQL

查看:47
本文介绍了如何在 MySQL 中将时间格式化为“2:34 小时"、“0:34 小时"等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个查询,该查询选择两次并计算它们之间的差异,其中一个是开始,另一个是找出持续时间的结束.但是两者都在 mysql 中设置为 TIME 类型.如何格式化时间而不是将 HH:MM:SS 显示为 '[OPTIONAL H]H':MM hrs'

I am writing a query that is selecting two times and calculating the difference between these, where one is the start and the other is the finish to find out the duration. However both are set to TIME types in mysql. How can i format the time instead of showing HH:MM:SS to '[OPTIONAL H]H':MM hrs'

我使用提取功能吗?我想我必须将 TIMEDIFF 包裹在其他东西上?

Do i use the extract function? I presume i have to wrap the TIMEDIFF around something else?

我的sql代码如下:

SELECT operator_no, log_in_time, TIMEDIFF(log_in_time,log_out_time) AS Duration

非常感谢

推荐答案

是的,用 TIME_FORMAT() 包裹:

SELECT operator_no, log_in_time,
       TIME_FORMAT(
           TIMEDIFF(log_in_time, log_out_time),
           '%H:%i hrs'
       ) AS Duration

这篇关于如何在 MySQL 中将时间格式化为“2:34 小时"、“0:34 小时"等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆