I have a system that connects to the database every 2-5 seconds when the user connects the application. Depending on its connection, the ping timeframe may be longer, for example, 10 seconds or so.
Example:
Pings: 1,4,6,8,9,12,16,20,50,180,187,189,200,203,206,210 ...
I run a query to capture ranges that do not exceed 1 minute between pings, group them, so I can tell how long the user has been connected:
Here is the query that I execute to select the results, as recommended by @fancyPants on this issue: MySQL query to group the results by date range?
select userid, groupnum, min(ping) as start_date, max(ping) as end_date, max(ping) - min(ping) as duration from ( select *, @groupnum := if(@prevUser != userId, @groupnum + 1, @groupnum), @groupnum := if(ping - @prevTS > 60, @groupnum + 1, @groupnum) as groupnum, @prevUser := userid, @prevTS := ping from Table1 t , (select @groupnum:=1, @prevTS:=NULL, @prevUser:=NULL) vars order by userid, ping ) sq group by userid, groupnum
Producing the following results:
user: X | start_date: 1 | end_date: 50 | duration: 49 user: X | start_date: 180 | end_date: 210 | duration: 30
I need help adding instructions to this request that will do the following.
first. Insert the selected rows into the new table using the same schema that the query returns:
id: auto_increment| user: X | start_date: 1 | end_date: 50 | duration: 49 id: auto_increment| user: X | start_date: 180 | end_date: 210 | duration: 30
second. Delete the selected rows that were selected in the query and inserted into the new table.
This request will be executed by cronjob on the server every 10 minutes. Therefore, I can clear the ping table, which will hit hard, and save in the new values โโthat we are going to display to our surfers.
In a new query, I will need a sentence to filter emails without expiration. Without expired pings, they are executed no more than 60 seconds before the current time when cron is running. For example, if now = 100, the last ping to capture cannot be less than 41. Thus, when cron is executed, I do not select rows from users who are still pinging to the database.
Could this be done in one request, or do I need two?
Thanks,