Welcome to WebmasterWorld Guest from 54.211.17.91

Forum Moderators: open

Message Too Old, No Replies

Help with DELETE statement

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

10+ Year Member



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 Jun 1, 2007 (gmt 0)

10+ Year Member



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)

 

Featured Threads

Hot Threads This Week

Hot Threads This Month