如何从mysql数据库中删除旧记录

时间:2012-09-25 22:03:37

标签: mysql

我正在尝试运行一个查询,该查询将删除数据库中除9个最新条目之外的所有条目。 uid是id_lv,表名为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');

我没有任何错误,但没有任何反应。

由于

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

包围错误。您错过了子查询中的结束),并且在DESC关键字之前不应该有逗号。

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