mysqldude

Get time difference between successive records

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Difference using @diff carried between rows (original)
original post
✓✓⚠ 1287⚠ 1287⚠ 1287✓✓✓✓
Difference using LAG() OVER (ORDER BY starttime) (modern)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
Difference using @diff carried between rows (original) original post
MySQL 5.6✓5.7✓8.0⚠ 12878.4⚠ 12879⚠ 1287
MariaDB 10.6✓10.11✓11.4✓11.8✓
Difference using LAG() OVER (ORDER BY starttime) (modern) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-23 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (2)
  • Difference using @diff carried between rows (original) (MySQL 8.0, MySQL 8.4, MySQL 9): warning 1287: Setting user variables within expressions is deprecated and will be removed in a future release. Consider alternatives: 'SET variable=expres
  • Difference using LAG() OVER (ORDER BY starttime) (modern) (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(ORDER BY starttime)), 0) AS diff FROM time_l

Related errors: 1287

The original article

First published 2012-10-04. The text below is the original; the table above shows how it behaves on current versions.

This post shows a solution to get the time difference between successive records in a table. If we are basing out solution on an primary key identifier say id, we cannot make the assumption that the id-1 represents the previous identifier (there may be gaps in the primary key). So using the ORDER BY clause should determine the order in which successive records appear. We can never assume any kind of order being returned, hence the importance of having the ORDER BY clause.

For this solution we make use of the user system variables to keep track of the previous row’s time.

In this example we have a table with a simple id and starttime. The second starttime represents the endtime for the first row and so on.

mysql> select * from time_links order by starttime;
+----+-----------+
| id | starttime |
+----+-----------+
|  5 | 09:00:30  |
|  7 | 09:01:00  |
|  8 | 09:01:01  |
|  6 | 09:01:59  |
|  2 | 09:09:01  |
|  3 | 09:09:30  |
|  4 | 09:09:59  |
|  1 | 17:05:23  |
+----+-----------+
8 rows in set (0.00 sec)

mysql> SELECT id,
    ->        Sec_to_time(@diff)                               AS starttime,
    ->        starttime                                        endtime,
    ->        IF(@diff = 0, 0, Time_to_sec(starttime) - @diff) AS diff,
    ->        @diff := Time_to_sec(starttime)
    -> FROM   time_links,
    ->        (SELECT @diff := 0) AS x
    -> ORDER  BY time_links.starttime;
+----+-----------+----------+-------+---------------------------------+
| id | starttime | endtime  | diff  | @diff := Time_to_sec(starttime) |
+----+-----------+----------+-------+---------------------------------+
|  5 | 00:00:00  | 09:00:30 |     0 |                           32430 |
|  7 | 09:00:30  | 09:01:00 |    30 |                           32460 |
|  8 | 09:01:00  | 09:01:01 |     1 |                           32461 |
|  6 | 09:01:01  | 09:01:59 |    58 |                           32519 |
|  2 | 09:01:59  | 09:09:01 |   422 |                           32941 |
|  3 | 09:09:01  | 09:09:30 |    29 |                           32970 |
|  4 | 09:09:30  | 09:09:59 |    29 |                           32999 |
|  1 | 09:09:59  | 17:05:23 | 28524 |                           61523 |
+----+-----------+----------+-------+---------------------------------+
8 rows in set (0.00 sec)