简体   繁体   中英

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

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)');

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)');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM