【问题标题】:mysql timestamp doesn't show Daysmysql时间戳不显示天
【发布时间】:2018-01-14 17:46:36
【问题描述】:

我正在使用 Date_format() 但由于某种原因它没有显示日期,它只显示小时。例如,如果时间是 51:01,它只会显示 03:00 (48+3),如果时间是 24:02,它只会显示 12:02。

有人知道我应该使用什么吗?我正在计算 2016 年和 2017 年工人的最长工作时间。

这是我的代码:

select Year(Date) AS Year,WorkerID, 
       Date_format(sec_to_time(sum(time_to_sec(goes)-time_to_sec(comes))), '%h:%i') as H 
FROM Info 
Group by Year, WorkerID

【问题讨论】:

  • %h 就是这样工作的。如果你想要几天,请使用%d
  • 然后我得到 7th, 12:02 :(

标签: mysql database date


【解决方案1】:

最简单的解决方案是使用十进制小时数:

select Year(Date) AS Year, WorkerID, 
       (sum(time_to_sec(goes) - time_to_sec(comes))) / 3600 as decimal_hours 
from Info 
Group by Year, WorkerID;

如果你真的想要,你可以使用 `concat 把它变成 HH:MM 格式:

select Year(Date) AS Year, WorkerID, 
       concat_ws(':',
                 floor( (sum(time_to_sec(goes) - time_to_sec(comes))) / 3600),
                 lpad(floor(sum(time_to_sec(goes) - time_to_sec(comes))) / 60) % 60, 2, '0')
                 )
from Info 
Group by Year, WorkerID;

【讨论】:

  • 感谢您的回答,但“lpad”出现错误,参数计数不正确
  • 和小数小时让我得到“0,0169”
猜你喜欢
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-23
  • 2018-03-19
  • 2021-01-04
  • 1970-01-01
相关资源
最近更新 更多