Hãy thử:
select
o1.id,
o1.operation_date_time,
(unix_timestamp(o2.operation_date_time) - unix_timestamp(o1.operation_date_time))
as duration
from operations as o1
inner join operations as o2
where o1.operation = "START"
and o2.operation = "STOP"
and o1.id = (o2.id - 1);
Nó sẽ cung cấp dưới dạng đầu ra:
+------+---------------------+----------+
| id | operation_date_time | duration |
+------+---------------------+----------+
| 1 | 2000-01-01 06:30:45 | 4455 |
| 3 | 2000-01-01 08:18:12 | 11146 |
| 5 | 2000-01-01 15:45:01 | 11792 |
+------+---------------------+----------+
3 rows in set (0.00 sec)