Assuming that you have a table called session_records
with columns id
, session_id
, and timestamp
(which stores the time the session was created), you can use the following query to remove all duplicate session records except for the most recent one:
DELETE FROM session_records
WHERE id NOT IN
(SELECT id FROM
(SELECT id, session_id, timestamp
FROM session_records
ORDER BY timestamp DESC)
AS t
GROUP BY session_id)
This query selects all session records, orders them by timestamp in descending order, and groups them by sessionid. Then it keeps only the records with the highest timestamp for each sessionid and deletes all the others.
Asked: 2023-07-07 14:32:10 +0000
Seen: 7 times
Last updated: Jul 07 '23