Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Help with DELETE statement

3:19 am on Jun 1, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 7, 2004
votes: 0

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!

10:54 pm on June 1, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 30, 2005
votes: 0

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)