如果大于25,试图删除记录?

时间:2013-12-03 19:00:54

标签: mysql

我正在尝试删除所有记录,因此我只有最后25条记录。我试图解决这个问题,但我似乎无法弄清楚,我的MySQL版本不支持大多数建议的DELETE LIMIT OFFSET,所以我正在努力

DELETE FROM
    account_versions
WHERE
    account_versions_id < (
        SELECT
            account_versions_id
        FROM
            account_versions
        ORDER BY
            account_versions_id
        DESC
        LIMIT 24, 1)
 AND
     account_id = 1

但我似乎无法让它发挥作用。我想只有account_id = 1

的最后25条记录

1 个答案:

答案 0 :(得分:0)

所以,正如你所说account_versions_id是一个自动增量。这就是你需要的:

DELETE FROM account_versions
 WHERE account_id = 1 
   and account_versions_id IN
      (select account_versions_id 
         from 
         (SELECT account_versions_id, @id:=@id+1 as rn
            FROM account_versions,
                 ( select @id:=0 ) a
           ORDER BY account_versions_id DESC) x
        where rn > 25) 

现在应该可以了。