| Welcome to WebmasterWorld Guest from 220.127.116.11 |
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
|Become a Pro Member|
|Help with DELETE statement|
Hi, I have a table in which items are added with a timestamp constantly. I need to delete * elements and leave on the table only the most recent 50 items.
How can I do that in a single and efficient statement? Thanks!
DELETE FROM table T1 WHERE timestamp_col<=(SELECT MAX(timestamp_col) FROM table T2 WHERE (SELECT COUNT(*) FROM table T3 WHERE T3.timestamp_col<=T2.timestamp_col<=50))
May or may not work (probably won't as it's off the top of my head...)
Or maybe something like
DELETE FROM table WHERE timestamp_col NOT IN (SELECT timestamp_col FROM table ORDER BY timstamp_col DESC LIMIT 50)
All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved