Query user session length on a timestamped activity log
Plan A:


Create a table with an AUTO_INCREMENT PK, plus the 2 columns in question.  (It can't be a TEMPORARY table due to an unfortunate limitation.)
Do a "self-join" to pair up adjacent rows.  Put this (id, user, and pair of times) in another table.
Filter -- keep pairs that are more than 1 hour.  (Need to also keep first and last times; this may involve UNION.)
Subtract adjacent id pairs to get session_length.  (This probably involves another table and another AUTO_INCREMENT, but could be done with an @variable.)  (Note:  Be aware of auto_increment_increment; if it is not 1, the subtract will be 'wrong'.)


So, Yes, it can be done in SQL; but do you want to go to this much work?

Plan B:  Somehow use @variables to count the session_length, watch out for change in userid, test for 1 hour, etc.

Plan C:  Migrate to MariaDB 10.2 and use its Windowing Functions.