code_junkie

MySQL5: Delete all but the 50 newest rows

Nov 11th, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. delete from table where id not in (
  2. select id from table
  3. order by id desc
  4. limit 50
  5. )
  6.  
  7. SELECT unixTime FROM entries ORDER BY unixTime DESC LIMIT 49, 1;
  8. DELETE FROM entries WHERE unixTime < $sqlResult;
  9.  
  10. SELECT timestampcol FROM table ORDER BY timestampcol DESC LIMIT 49,1;
  11.  
  12. DELETE FROM table WHERE timestampcol < ( SELECT timestampcol FROM table ORDER BY timestampcol DSEC LIMIT 49,1 )
  13.  
  14. DELETE FROM _table_
  15. WHERE _date_ NOT IN (SELECT _date_ FROM _table_ ORDER BY _date_ DESC LIMIT 50)
Add Comment
Please, Sign In to add comment