You can't specify target table 't_coupon_new ' for update in FROM clause
在给mysql做数据处理时,一开始写了如下sql来删除重复导入的数据:
delete from t_coupon_new where id in( select e.id
from t_coupon_new e,t_coupon_new f where e.sourceid=f.sourceid and
e.source=f.source and e.createtime<f.createtime)
但一执行,出现标题所示的异常。
百度一下,知道了原因:这个异常的 大概意思就是 对于更新中的from语句,你指定不了一个目标表 t_coupon_new 。 大概就是 随着你的删除操作 前面的重复条件查询结果集也在动态的跟新,这样如果还允许你删除的话,后果是无法想象的,所以这时不能让你删除数据。 数据库系统原理 上是这么说的Problem: as we delete tuples from deposit, the average balance changes Solution used in SQL: 1. First, compute avg balance and find all tuples to delete 2. Next, delete all tuples found above (without recomputing avg or
retesting the tuples)
如是,修改了一下SQL为:
delete from t_coupon_new where id in( select id from (select e.id id
from t_coupon_new e,t_coupon_new f where e.sourceid=f.sourceid and
e.source=f.source and e.createtime<f.createtime) a);
这样就好了。解决的原理就是:
就是让要查询出来的id集合不随删除 过程而动态的改变。 所以,1、先查询出所有重复的数据里,较早的那个数据的id作为一个结果集保存 2、再来根据这个结果集做删除操作。这样就好了。
不知道大家碰到是不是这样解决的,或者还有更好的解决的办法。一定要告诉我哦。
在给mysql做数据处理时,一开始写了如下sql来删除重复导入的数据:
delete from t_coupon_new where id in( select e.id
from t_coupon_new e,t_coupon_new f where e.sourceid=f.sourceid and
e.source=f.source and e.createtime<f.createtime)
但一执行,出现标题所示的异常。
百度一下,知道了原因:这个异常的 大概意思就是 对于更新中的from语句,你指定不了一个目标表 t_coupon_new 。 大概就是 随着你的删除操作 前面的重复条件查询结果集也在动态的跟新,这样如果还允许你删除的话,后果是无法想象的,所以这时不能让你删除数据。 数据库系统原理 上是这么说的Problem: as we delete tuples from deposit, the average balance changes Solution used in SQL: 1. First, compute avg balance and find all tuples to delete 2. Next, delete all tuples found above (without recomputing avg or
retesting the tuples)
如是,修改了一下SQL为:
delete from t_coupon_new where id in( select id from (select e.id id
from t_coupon_new e,t_coupon_new f where e.sourceid=f.sourceid and
e.source=f.source and e.createtime<f.createtime) a);
这样就好了。解决的原理就是:
就是让要查询出来的id集合不随删除 过程而动态的改变。 所以,1、先查询出所有重复的数据里,较早的那个数据的id作为一个结果集保存 2、再来根据这个结果集做删除操作。这样就好了。
不知道大家碰到是不是这样解决的,或者还有更好的解决的办法。一定要告诉我哦。