tatty27
tatty27

Reputation: 1554

How to delete old records from mysql database

I am trying to run a query that will delete all of the entries in a database except the 9 most recent entries. The uid is id_lv and the table is called last_viewed

mysql_query('DELETE FROM last_viewed WHERE id_lv NOT IN (SELECT id_lv FROM last_viewed ORDER BY id_lv, desc LIMIT 0, 9');

I get no errors at all but nothing happens.

Thanks

Upvotes: 0

Views: 342

Answers (2)

Scott Selby
Scott Selby

Reputation: 9580

mysql_query('DELETE FROM last_viewed 
               WHERE id_lv NOT IN 
              (SELECT id_lv FROM last_viewed 
               ORDER BY id_lv desc LIMIT 0, 9)');

Upvotes: 1

RichardTheKiwi
RichardTheKiwi

Reputation: 107826

Bracketing error. You were missing a closing ) in the subquery and there shouldn't be a comma before the DESC keyword.

mysql_query('DELETE FROM last_viewed
             WHERE id_lv NOT IN (
                 SELECT id_lv
                 FROM last_viewed
                 ORDER BY id_lv desc
                 LIMIT 0, 9)');

Upvotes: 0

Related Questions