使用限制和偏移删除行不使用子句
我想用偏移删除行,因此我被迫使用嵌套查询,因为它在原始删除子句中不支持。
我知道这将有效(ID是主要键):
DELETE FROM customers
WHERE ID IN (
SELECT ID
FROM customers
WHERE register_date > '2012-01-01'
ORDER BY register_date ASC
LIMIT 5, 10
);
但是,当我获得错误
This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'.
服务器版本时,这是我的版本中不支持的:10.4.22-mariaDB
我该怎么做才能达到与上面支持的相同结果版本。
CREATE TABLE customers (
ID INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(32) NOT NULL,
REGISTER_DATE DATETIME NOT NULL
);
I want to delete rows with an offset, so I am forced to use a nested query since its not support in the raw DELETE clause.
I know this would worked (ID is the primary key):
DELETE FROM customers
WHERE ID IN (
SELECT ID
FROM customers
WHERE register_date > '2012-01-01'
ORDER BY register_date ASC
LIMIT 5, 10
);
However, this is unsupported in my version as I get the error
This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'.
Server version: 10.4.22-MariaDB
What can I do to achieve the same result as above that is supported in my version.
CREATE TABLE customers (
ID INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(32) NOT NULL,
REGISTER_DATE DATETIME NOT NULL
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我没有错过一些简单的删除,即可完成工作...
If I did not miss something a simple delete with join will do the job...
Here is a demo for your version of MariaDB
将表加入使用
row_number()
窗口函数对行进行排序并过滤要删除的行:请参阅
Join the table to a subquery that uses
ROW_NUMBER()
window function to sort the rows and filter the rows that you want to be deleted:See the demo.
您可以尝试使用
row_number
窗口功能,然后捕获那些等级位置在5到15之间的行。尽管取得了相同的结果,但这将安全避免使用
limit
。编辑。加入。
You could try assigning a rank to your rows with the
ROW_NUMBER
window function, then catch those rows whose rank position is between 5 and 15.This would safely avoid the use of
LIMIT
, though achieves the same result.EDIT. Doing it with a join.