mysql删除语句中有子查询表名与删除表名相同的报错的解决

SQL Error [1093] [HY000]: Table ‘SIGN_IN_SCORE’ is specified twice, both as a target for ‘DELETE’ and as a separate source for data   Table ‘SIGN_IN_SCORE’ is specified twice, both as a target for ‘DELETE’ and as a separate source for data

delete from a where a.id not in(select a.id from a where a.createDate > now());

在这种情况下 会有问题。提示指定了相同的表名两次,那么如果我们的执行sql的用户有建表和删表的权限的时候,可以用如下的语句进行替换。

create table tempa (select a.id from a where a.createDate > now());

delete from a where a.id not in(select id from tempa);

drop table tempa;