码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2016-09-21 12:56:53      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

使用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

标签:

原文地址:http://www.cnblogs.com/pokid/p/5892030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!