0

I have a table with (amongst others) 2 columns (sayA and B) with a primary key and one with a timestamp (say C). I would like to delete everything but the last XXX entries (when sorted by the timestamp). Could anyone help me out with the syntax? I guess it should be along the lines of:

DELETE FROM Table WHERE A, B NOT IN (SELECT A, B FROM Table ORDER BY C DESC LIMIT XXX)

Is that correct SQL?

Edit: Somehow this question got closed being a duplicate. The referred question however only has answers that apply to a table which has exactly one primary key (or index) .. how do I expand this to a table where there are two?

Lieuwe
  • 1,734
  • 2
  • 27
  • 41

1 Answers1

0

You can try this

DELETE FROM TABLE T1 INNER JOIN (SELECT A, B FROM TABLE ORDER BY C DESC LIMIT XXXX) AS T2 ON T1.A = T2.A AND T1.B = T2.B 

in interior select you define conditions for delete.

Jasmin Kurtic
  • 141
  • 12
  • But that deletes XXXXX entries doesn't it? I want to delete everything but those XXXXX entries. – Lieuwe Aug 15 '16 at 10:13
  • maybe you can DELETE FROM TABLE T1 left JOIN (SELECT A, B FROM TABLE ORDER BY C DESC LIMIT XXXX) AS T2 ON T1.A = T2.A AND T1.B = T2.B where T2.A is null AND T2.B is null now you are delete where row is not join – Jasmin Kurtic Aug 15 '16 at 10:40