Im trying to calculate the time difference between an user login/logout, to do so, I need to do following operation: 2nd Login - 1st Logout, i don't know even how to explain to correctly so i ll post the table result here to illustrate:
Asked
Active
Viewed 1,458 times
0

Mureinik
- 297,002
- 52
- 306
- 350

Davi Steewel
- 3
- 2
-
Is this what you need? http://stackoverflow.com/questions/10907750/calculate-difference-between-two-datetimes-in-mysql or http://stackoverflow.com/questions/4759248/difference-between-two-dates-in-mysql – trainoasis Oct 08 '14 at 12:53
-
Try putting sample data and desired results into your question. – Gordon Linoff Oct 08 '14 at 12:53
-
1Can you share your table(s)' structure(s) please? – Mureinik Oct 08 '14 at 12:54
-
@GordonLinoff The table is this one: http://i.stack.imgur.com/Iz1Nh.jpg the result that i want is the 3rd column "Difference", this should be the result of the SELECT... to obtain the "00:06:38" I did **Login 2nd row** (06/10/2014 14:21) - **logout 1st row** (06/10/2014 14:15) – Davi Steewel Oct 08 '14 at 13:41
1 Answers
1
You can acomplish this by left joining the table with itself. This query renders the table you posted:
SELECT t.Login, t.Logout, TIMEDIFF(MIN(t2.Login), t.Logout) AS Difference
FROM my_table t
LEFT JOIN my_table t2
ON t.Logout < t2.Login
GROUP BY t.Logout
MIN(t2.Login) is the next login after t.Logout

iag
- 166
- 4