像这样的东西可能会奏效:
select ticket_id, min(a.time_created), max(b.time_created),
TIMESTAMPDIFF(HOUR,min(a.time_created), max(b.time_created))
from tbl AS a JOIN tbl AS b USING(ticket_id)
where a.time_created < (b.time_created - INTERVAL 1 DAY)
group by(a.ticket_id);
生产:
+------------+--------------------------------+--------------------------------+--------------------------------------------------------------+
| TICKET_ID | MIN(A.TIME_CREATED) | MAX(B.TIME_CREATED) | TIMESTAMPDIFF(HOUR,MIN(A.TIME_CREATED), MAX(B.TIME_CREATED)) |
+------------+--------------------------------+--------------------------------+--------------------------------------------------------------+
| 1 | August, 17 2013 12:00:00+0000 | August, 19 2013 12:11:00+0000 | 48 |
+------------+--------------------------------+--------------------------------+--------------------------------------------------------------+
见http://sqlfiddle.com/#!2/4af76e/7
请注意我TIMESTAMPDIFF
在此处使用,请求HOUR
. TIMEDIFF
不知道您的域规范,由于值范围有限TIME
(≈ 34 天),我不在这里使用。