我对按某些列删除前 N 行顺序有些困惑。
我在这里创建了一个示例Example at fiddle
这些查询有什么问题?
Delete Top(3) from Table1 order by id desc
Delete Top(3) from Table1
where id IN (select id from Table1 order by id desc)
由于在 mysql 中,limit 关键字可以很好地完成这项工作
我对按某些列删除前 N 行顺序有些困惑。
我在这里创建了一个示例Example at fiddle
这些查询有什么问题?
Delete Top(3) from Table1 order by id desc
Delete Top(3) from Table1
where id IN (select id from Table1 order by id desc)
由于在 mysql 中,limit 关键字可以很好地完成这项工作
您可以使用 CTE 进行更快的有序删除,而无需单独的子查询来检索前 3 个 id。
WITH T
AS (SELECT TOP 3 *
FROM Table1
ORDER BY id DESC)
DELETE FROM T
将top 3
子句添加到子选择中:
Delete from Table1
where id IN (
select top 3 id
from Table1
order by id desc
)