0

I am running the following query, but there is one random result from the TIMESTAMPDIFF function that is returning a negative value, only one:

select JOB_ID,FROM_UNIXTIME(TIME_STARTED/1000) as TIME_STARTED,FROM_UNIXTIME(TIME_FINISHED/1000) as TIME_FINISHED,TIMESTAMPDIFF(SECOND,FROM_UNIXTIME(TIME_STARTED/1000, '%Y-%m-%d %h:%i:%s'),FROM_UNIXTIME(TIME_FINISHED/1000, '%Y-%m-%d %h:%i:%s')) AS DURATION from JOB;

Here's some of the data returned:

| job_201306051933_0707 | 2013-06-10 23:16:57 | 2013-06-10 23:17:06 |        9 |
| job_201306051933_0832 | 2013-06-11 10:00:47 | 2013-06-11 10:29:03 |     1696 |
| job_201306051933_0850 | 2013-06-11 12:49:57 | 2013-06-11 13:18:30 |   -41487 |

Again, that last one is the only negative value returned and I'm not sure why this is. any help is appreciated.

4

1 回答 1

1

您正在使用默认格式FROM_UNIXTIME()来显示 time_started 和 time_finished,但要计算时差,您需要将时间戳转换为'%Y-%m-%d %h:%i:%s'格式。

在这种格式%h中,表示小时 01-12(上午或下午),因此最后一行的 time_finished 被视为“2013-06-11 01:18:30”。它是比同一行中的 time_started 更早的时间戳,因此您会得到否定的结果。

您应该使用`%H'而不是'%h'几个小时来获得 00-23 范围内的正确值。或者使用与显示时间戳相同的默认格式。

于 2013-06-17T15:21:46.180 回答