I am trying to make the difference of two rows in an mysql database.
I have this table containing ID, kilometers, date, car_id, car_driver etc...
Since I don\'t alwa
With MySQL 8 you can use CTE and ROW_NUMBER window function to make a more readable query
WITH cte_name AS (
SELECT
ROW_NUMBER() OVER (ORDER BY update_time) as row_num,
id,
other_data,
update_time
FROM table_name WHERE condition = 'some_condition'
)
SELECT t2.id, t2.other_data, TIMEDIFF(t2.update_time, t1.update_time) AS time_taken
FROM
cte_name t1
JOIN cte_name t2 ON t1.row_num = t2.row_num-1
ORDER BY time_taken;
In this example I'm trying get the difference between datetime values.
There are some good tutorials for: CTE (Common Table Expression), ROW_NUMBER and even window functions