当我尝试删除一些行时,在 ORDER BY 附近使用正确的语法
Right syntax to use near ORDER BY when I try to delete some number of rows
当我尝试使用 ORDER BY ASC 和 LIMIT = 2 从表中删除某些行时(从连接表中获取的行)(例如:如果我有 10 条记录,则需要删除前 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 sales_flat_quote.entity_id ASC LIMIT 2' at line 9
下面是我的代码:
DELETE table1,table2,table3
FROM table1
LEFT JOIN table2 on table1.entity_id=table2.quote_id
LEFT JOIN table3 on table1.entity_id=table3.quote_id
WHERE table1.entity_id <= 101
ORDER BY table1.entity_id ASC LIMIT 2;
您正在执行多项 table 删除和文档 states the following:
You cannot use ORDER BY or LIMIT in a multiple-table DELETE.
嵌套选择最终可能会出现一些解决方法。检查 this answer 类似的问题。
如果 temp table 对你来说是接受的table,你可以试试这样的方法。不是很好但很简单的解决方案。
CREATE TEMPORARY TABLE TempToBeDeleted (
id_table1 INT NOT NULL
, id_table2 INT NOT NULL
, id_table3 INT NOT NULL
);
INSERT INTO (id_table1, id_table2, id_table3)
SELECT DISTINCT table1.primary_key, table2.primary_key, table3.primary_key
FROM
table1
LEFT JOIN table2 ON table1.entity_id = table2.quote_id
LEFT JOIN table3 ON table1.entity_id = table3.quote_id
WHERE table1.entity_id <= 101
ORDER BY table1.entity_id ASC LIMIT 2;
DELETE FROM table1, table2, table3
FROM
TempToBeDeleted
INNER JOIN table1 ON TempToBeDeleted.id_table1 = table1.primary_key
INNER JOIN table2 ON TempToBeDeleted.id_table2 = table2.primary_key
INNER JOIN table3 ON TempToBeDeleted.id_table3 = table3.primary_key
;
DROP TABLE TempToBeDeleted;
解法:
DELETE table1,table2,table3
FROM table1
INNER JOIN (
SELECT entity_id
FROM table1
WHERE entity_id<= 101
ORDER BY entity_id ASC
LIMIT 2
) sub1
ON table1.entity_id=sub1.entity_id
LEFT JOIN table2 ON table2.quote_id = table1.entity_id
LEFT JOIN table3 ON table1.entity_id = table3.quote_id
当我尝试使用 ORDER BY ASC 和 LIMIT = 2 从表中删除某些行时(从连接表中获取的行)(例如:如果我有 10 条记录,则需要删除前 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 sales_flat_quote.entity_id ASC LIMIT 2' at line 9
下面是我的代码:
DELETE table1,table2,table3
FROM table1
LEFT JOIN table2 on table1.entity_id=table2.quote_id
LEFT JOIN table3 on table1.entity_id=table3.quote_id
WHERE table1.entity_id <= 101
ORDER BY table1.entity_id ASC LIMIT 2;
您正在执行多项 table 删除和文档 states the following:
You cannot use ORDER BY or LIMIT in a multiple-table DELETE.
嵌套选择最终可能会出现一些解决方法。检查 this answer 类似的问题。
如果 temp table 对你来说是接受的table,你可以试试这样的方法。不是很好但很简单的解决方案。
CREATE TEMPORARY TABLE TempToBeDeleted (
id_table1 INT NOT NULL
, id_table2 INT NOT NULL
, id_table3 INT NOT NULL
);
INSERT INTO (id_table1, id_table2, id_table3)
SELECT DISTINCT table1.primary_key, table2.primary_key, table3.primary_key
FROM
table1
LEFT JOIN table2 ON table1.entity_id = table2.quote_id
LEFT JOIN table3 ON table1.entity_id = table3.quote_id
WHERE table1.entity_id <= 101
ORDER BY table1.entity_id ASC LIMIT 2;
DELETE FROM table1, table2, table3
FROM
TempToBeDeleted
INNER JOIN table1 ON TempToBeDeleted.id_table1 = table1.primary_key
INNER JOIN table2 ON TempToBeDeleted.id_table2 = table2.primary_key
INNER JOIN table3 ON TempToBeDeleted.id_table3 = table3.primary_key
;
DROP TABLE TempToBeDeleted;
解法:
DELETE table1,table2,table3
FROM table1
INNER JOIN (
SELECT entity_id
FROM table1
WHERE entity_id<= 101
ORDER BY entity_id ASC
LIMIT 2
) sub1
ON table1.entity_id=sub1.entity_id
LEFT JOIN table2 ON table2.quote_id = table1.entity_id
LEFT JOIN table3 ON table1.entity_id = table3.quote_id