MySQL's documentation states the following
Regarding the "single-table" synatax:
If the ORDER BY clause is specified, the rows are deleted in the order that is specified. The LIMIT clause places a limit on the number of rows that can be deleted.
Regarding the "multi-table" syntax:
For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.
Some testing is revealing in the matter of the ORDER BY
limitations.
This is a valid single-table DELETE
statement with an ORDER BY
:
DELETE FROM table
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
A similar query with an explicit joining condition against one other table results in a syntax error on the ORDER BY
, despite only one of the tables being targeted for deletion:
DELETE table1
FROM table1 JOIN table2 ON table1.id = table2.id
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY timestamp LIMIT 2' at line 1
Specifying the same query with an implicit join fails the same way
DELETE table1
FROM table1, table2
WHERE
table1.id = table2.id
AND somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY timestamp LIMIT 2' at line 1
Specifying no table after DELETE
for deletion in a joined (multi-table) statement so it looks more like a single-table syntax is also a syntax error
DELETE /* no table named here */
FROM table1 JOIN table2 ON table1.id = table2.id
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE somecol
It's the table named in the DELETE
clause:
Finally, using only one table, but with the mult-table syntax (naming a table after the DELETE
keyword) does not permit an ORDER BY
, so the true identifying difference here appears to be tables named in the DELETE
clause to distinguish multi-table from single-table:
This query only involves one table (without a join), but produces a syntax error:
/* name table1 in the DELETE clause */
DELETE table1
/* but not other table is joined */
FROM table1
WHERE somecol = 'someval'
ORDER BY id LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY id LIMIT 1