My table:
_id | state | timeStamp
I want select all the rows that are of state=0 and order that result by timeStamp. I than want to delete all items passed a particular limit, say 50 rows.
*Essentially, I don’t want there to be more than 50 rows with the state=0
How can I achieve this? I tried writing one but I’m getting a bit lost…
DELETE FROM table WHERE (state=0) ORDER BY timeStamp
Use the
NOT INstatement with a sub query:DELETE FROM table WHERE state = 0 AND _id NOT IN(SELECT _id FROM table WHERE state = 0 ORDER BY timeStamp LIMIT 50);What it does is select all rows where
state = 0and then removes the 50 first rows that hasstate = 0all while ordering on timeStamp