我想从MySQL5.7中删除Duplicate数据,这是我的sql:
DELETE FROM wallet_consume_record
WHERE trans_no IN (
SELECT trans_no
FROM wallet_consume_record
GROUP BY trans_no
HAVING count(trans_no) > 1)
AND id NOT IN (
SELECT min(id)
FROM wallet_consume_record
GROUP BY trans_no
HAVING count(trans_no) > 1
);
但当我执行它时,它抛出:
(1093, u"You can't specify target table 'wallet_consume_record' for update in FROM clause")
我已经试过了:
SET optimizer_switch = 'derived_merge=off';
为什么以及如何让它工作?我还试着 Package :
DELETE FROM wallet_consume_record
WHERE trans_no IN (
SELECT trans_no
FROM wallet_consume_record
GROUP BY trans_no
HAVING count(trans_no) > 1)
AND id NOT IN (
select c.id from
(
SELECT min(id) as id
FROM wallet_consume_record
GROUP BY trans_no
HAVING count(trans_no) > 1
) as c
);
还是不行。
mysql root@10.244.5.47:meow_report_pro> select version();
+-------------+
| version() |
|-------------|
| 5.7.29-log |
+-------------+
1 row in set
Time: 0.001s
2条答案
按热度按时间wdebmtf21#
mysql不接受引用数据库中正在更新或删除的表
from
同一查询的子句。你可以用一个
join
取而代之的是:zynd9foi2#
您可以使用组\u concat。
创建两个用户定义的函数,返回记录的组concat值,并在查询中使用。
i、 e(其中一个函数看起来像)
您可以简单地在查询中使用它
对于非in值,可以使用相同的值