1

如果我有开始时间和结束时间,并且我正在找到总时间,如果超过 24 小时,我希望它而不是显示 26,显示 1 天和 2 小时。如何才能做到这一点?

4

3 回答 3

2

你可能想做一个

IF    (MOD(HOUR(TIME) / 24) )  > 24, 
    SELECT CONCAT(
    FLOOR(HOUR(TIME) / 24), ' days ',
    MOD(HOUR(TIME), 24), ' hours ',
    MINUTE(TIME), ' minutes')
)

Timediff,

FLOOR(HOUR(TIMEDIFF('2010-01-06 08:46', '2010-01-01 12:30')) / 24), ' days ',
于 2012-12-02T19:47:30.710 回答
0

如果您的start_timeend_time是日期时间类型,则使用TIMEDIFF 函数并应用所需的 DATE_FORMAT。

例如

select DATE_FORMAT(TIMEDIFF('2012:01:01 00:00:00', '2012:01:01 00:00:00.000001'),
                  '%d: %H: %m');
于 2012-12-02T19:47:42.127 回答
0

这对我有用:

select
   o.id,
   o.created,
   o.complete_date,
   concat(timestampdiff(day, o.created, o.complete_date),
          'd ',
          lpad(
             mod(timestampdiff(hour, o.created, o.complete_date), 24),
             2, '0'),
          'h'
   ) as time_to_complete,
from
   orders o
;

结果:

+---------+---------------------+---------------------+------------------+
| id      | created             | complete_date       | time_to_complete |
+---------+---------------------+---------------------+------------------+
| 2393721 | 2018-02-20 00:01:44 | 2018-02-21 02:32:08 | 1d 02h           |
| 2393722 | 2018-02-20 00:02:56 | 2018-02-21 00:32:08 | 1d 00h           |
| 2393723 | 2018-02-20 00:04:12 | 2018-02-20 21:32:07 | 0d 21h           |
| 2393724 | 2018-02-20 00:04:50 | 2018-02-20 23:47:05 | 0d 23h           |
| 2393725 | 2018-02-20 00:07:07 | 2018-02-20 17:32:03 | 0d 17h           |
| 2393726 | 2018-02-20 00:13:41 | 2018-02-21 00:32:08 | 1d 00h           |
| 2393727 | 2018-02-20 00:14:25 | 2018-02-21 00:32:08 | 1d 00h           |
| 2393728 | 2018-02-20 00:16:47 | 2018-02-20 23:32:04 | 0d 23h           |
| 2393729 | 2018-02-20 00:17:19 | 2018-02-20 23:47:09 | 0d 23h           |
| 2393730 | 2018-02-20 00:17:59 | 2018-02-21 01:32:08 | 1d 01h           |
+---------+---------------------+---------------------+------------------+
于 2018-02-28T21:21:45.820 回答