I have the following mysql table which records user visit times:
I'd like to update visit_id so that a "visit" is a group of entries for the same user where no entry is more than 20 minutes after the previous, and the visit_id for each entry is the visit_id of the first entry of that visit.
I hope that this example helps make it clear. After the update, the example table above should become:
I'm able to create an update which sets the visit_id to the visit_id of the previous entry for that user if the previous entry occurred less than 20 minutes before.
But I can't solve how to create a sql-only update to deal with the "cascading" effect, in other words, that the visit_id needs to find the earliest entry for that user_id which occurs before a 20-minute gap. It might be the visit_id of that entry (if it's the first of that visit), or the visit_id of previous entry, or of the previous-previous, or of the previous-previous-previous-previous, etc.
How could I write this update?