Search code examples
mysqlsqldatabasesql-deletemariadb-10.4

Deleting rows using a limit and offset without using IN clause


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

Solution

  • If I did not miss something a simple delete with join will do the job...

    delete customers
    from (select *
          from customers
          WHERE register_date > '2012-01-01'
          order by register_date asc
          limit 5, 2) customers2 
    join customers on customers.id = customers2.id
    

    Here is a demo for your version of MariaDB