首页 > 代码库 > mysql中You can't specify target table for update in FROM clause

mysql中You can't specify target table for update in FROM clause

使用mysql在删除表中重复记录

delete from user where username in (select user name form(select username from user group by username having count(username)>1));

遇到mysql报错You can‘t specify target table for update in FROM clause

上网百度了下原来是mysql中不允许先select出同一表中的某些值,再update这个表(在同一语句中),这个问题只出现于mysql,sqlserver和oracle不会出现此问题

解决办法:将select出的结果再通过中间表select一遍

delete from user where username in (select a.username from(select username from user group by username having count(username)>1)a);

这样delete和select的就不是同一个表了

mysql中You can't specify target table for update in FROM clause