1

do you think a query like this will create problem in the execution of my software? I need to delete the all the table, except the last 2 groups of entries, grouped by the same time of insert.

delete from tableA WHERE time not in
                (
                  SELECT time FROM
                  (select distinct time from tableA order by time desc limit 2 
                  ) AS tmptable
                );

Do you have better solution? I'm using mysql 5.5

1 Answer 1

2

I don't see anything wrong with your query, but I prefer using an OUTER JOIN/NULL check (plus it alleviates the need for one of the nested subqueries):

delete a
from tableA a 
  left join 
  (
      select distinct time 
      from tableA 
      order by time desc 
      limit 2 
  ) b on a.time = b.time
where b.time is null
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.