是否可以构建单个 mysql 查询(不带变量)以从表中删除所有记录,但最新的 N(按 id desc 排序)除外?
Is it possible to build a single mysql query (without variables) to remove all records from the table, except latest N (sorted by id desc)?
像这样的东西,只是它不起作用:)
Something like this, only it doesn't work :)
delete from table order by id ASC limit ((select count(*) from table ) - N)
谢谢.
您不能以这种方式删除记录,主要问题是您不能使用子查询来指定 LIMIT 子句的值.
You cannot delete the records that way, the main issue being that you cannot use a subquery to specify the value of a LIMIT clause.
这有效(在 MySQL 5.0.67 中测试):
This works (tested in MySQL 5.0.67):
DELETE FROM `table`
WHERE id NOT IN (
SELECT id
FROM (
SELECT id
FROM `table`
ORDER BY id DESC
LIMIT 42 -- keep this many records
) foo
);
中间子查询是必需的.没有它,我们会遇到两个错误:
The intermediate subquery is required. Without it we'd run into two errors:
幸运的是,使用中间子查询可以让我们绕过这两个限制.
Fortunately, using an intermediate subquery allows us to bypass both of these limitations.
Nicole 指出这个查询可以针对某些用例(例如这个)进行显着优化.我建议您也阅读该答案,看看它是否适合您.
Nicole has pointed out this query can be optimised significantly for certain use cases (such as this one). I recommend reading that answer as well to see if it fits yours.
这篇关于SQL 查询:删除表中除最新 N 之外的所有记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!