查询:
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
。
如何解决这个问题?请帮忙。
答案 0 :(得分:0)
在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
);