Ask Your Question
1

How can I remove all duplicate session records except for the most recent one?

asked 2023-07-07 14:32:10 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-07 14:51:01 +0000

lakamha gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-07 14:32:10 +0000

Seen: 7 times

Last updated: Jul 07 '23