List:General Discussion« Previous MessageNext Message »
From:M Wells Date:April 15 2003 7:26pm
Subject:Delete all but (n) most recent records?
View as plain text  
Hi All,

Just wondering if anyone can help me work out if it's possible to write
a query that deletes all but the (n) most recent records in a table,
using a timestamp field to determine record order?
 
For the sake of the argument, let's say I have a table that currently
has 500 records in it and I want to retain the most recent 200 records,
therefore deleting the oldest 300 records (though, I only know ahead of
time that I want to keep the most recent (n) records, i.e. 200 in this
example, I don't know how many records there are in the table and at
times there may actually be less that 200 records, meaning all records
should remain).

Is this possible using just SQL, or do I need to do some of the work in
PHP?

Any help appreciated!

Warm regards,

Murray Wells
http://www.planetthoughtful.org

Mysql, query, sql


Thread
Delete all but (n) most recent records?M Wells15 Apr
  • RE: Delete all but (n) most recent records?Peter Monk16 Apr
    • Re: Delete all but (n) most recent records?Daniel Kasak16 Apr
      • Re: Delete all but (n) most recent records?Don Read16 Apr
        • Re: Delete all but (n) most recent records?Daniel Kasak16 Apr
          • Re: Delete all but (n) most recent records?Steve Edberg16 Apr
            • Re: Delete all but (n) most recent records?Stephen Giese16 Apr
        • Re: Delete all but (n) most recent records?Santino16 Apr
        • Re: Delete all but (n) most recent records?Santino16 Apr
          • RE: Delete all but (n) most recent records?Jon Frisby16 Apr
            • RE: Delete all but (n) most recent records?Jon Frisby16 Apr
              • RE: Delete all but (n) most recent records?M Wells19 Apr
                • RE: Delete all but (n) most recent records?Jon Frisby19 Apr
          • RE: Delete all but (n) most recent records?M Wells19 Apr
            • Re: Delete all but (n) most recent records?Peter Brawley19 Apr
        • Re: Delete all but (n) most recent records?Jerry17 Jul
RE: Delete all but (n) most recent records?Andrew Braithwaite16 Apr