从TIMESTAMPDIFF中提取hh:mm:ss(时间,Current_Time())

时间:2018-07-19 08:13:11

标签: mysql sql datetime

CREATE TABLE Test (

id int primary key,
Present varchar(10),
Date date,
Time time
);

INSERT INTO Test (id, Present, Date, Time)
Values (1, 'Present', '2018-07-18', '10:13:55' ),
(2, 'Present', '2018-07-18', '10:10:55' );

查询:

SELECT 
id,
Present,
Date,
Time,
TIMESTAMPDIFF(MINUTE, time, CURRENT_TIMESTAMP())
FROM Test

结果:

| id | Present |       Date |     Time | TimeDifference |
|----|---------|------------|----------|----------------|
|  1 | Present | 2018-07-18 | 10:13:55 |           -127 |
|  2 | Present | 2018-07-18 | 10:10:55 |           -124 |

我希望将时差显示为hh:mm:ss,而不仅仅是分钟。我尝试过:

SEC_TO_TIME(TIMESTAMPDIFF(SECOND, time, CURRENT_TIMESTAMP())) AS TimeDifference 

但是这给了我错误:“第5列中时间'-02:03:10'的格式错误”

如何从TIMESTAMPDIFF函数中提取此信息?我在网上到处都看过,但是找不到任何东西。

1 个答案:

答案 0 :(得分:1)

在时间和CURRENT_TIMESTAMP之间切换,您将获得一个正数

将单位时间更改为秒,然后将diff秒转换为时间。

  

SEC_TO_TIME()函数将数字秒转换为时间值(格式为HH:MM:SS)。

SELECT 
id,
Present,
Date,
Time,
SEC_TO_TIME(TIMESTAMPDIFF(SECOND,CURRENT_TIMESTAMP(),time))
FROM Test