查询:
DELETE FROM orders WHERE sales_id IN (
SELECT sales_id FROM orders GROUP BY sales_id HAVING COUNT(*) > 1
);
有一个错误:
Error Code: 1093. You can't specify target table 'orders' for update in FROM clause
。
如何解决这个问题?请帮忙。
查询:
DELETE FROM orders WHERE sales_id IN (
SELECT sales_id FROM orders GROUP BY sales_id HAVING COUNT(*) > 1
);
有一个错误:
Error Code: 1093. You can't specify target table 'orders' for update in FROM clause
。
如何解决这个问题?请帮忙。
在 MySQL 中,您不能修改在 SELECT 部分中使用的同一个表。此行为记录在:http ://dev.mysql.com/doc/refman/5.6/en/update.html
您将需要停止使用嵌套子查询并分两部分执行操作,或者使用简单的 where 子句。
如果您想使用子查询,则可以尝试在子查询中为表指定别名,如下所示:
DELETE FROM orders WHERE sales_id IN (
SELECT sales_id FROM (
SELECT * FROM orders
) AS t1 GROUP BY sales_id HAVING COUNT(*) > 1
);