MySql报错: You can't specify target table 'table name' for update in FROM clause

在Mysql下执行:
delete from blur_article where id not in(select min(id) from blur_article group by title)

用途是去重复标题,但是却报错!
#1093 - You can't specify target table 'blur_article' for update in FROM clause

在Mysql下执行:
select * from blur_article where id not in(select min(id) from blur_article group by title)
执行查询语句却显示成功!

怎么回事呀,如果上面的delete不能执行,有没有别的sql可以做这样的操作?
我是想做一个去重复操作,比如说:
字段 id title
1 张三
2 李四
3 张三
4 王五
5 李四

最终结果是
id title
1 张三
2 李四
4 王五

错误提示:不能先将select出表中的某些值,再update这个表(在同一语句中)。

替换方案:

方案一:

多嵌套一层子查询,再进行删除,如下:

完整代码如下:

DELETE FROM blur_article WHERE id NOT IN (

SELECT id FROM (

SELECT min(id) AS id FROM blur_article GROUP BY title

) t

)

方案二:

1.创建一张临时表,将要删除的条件自动存入临时表中:

2.再根据临时表,删除主表数据:

3.最后删除掉临时表:

完整代码如下:

1.create table temp as select min(id) as col1 from blur_article group by title;

2.delete from blur_article where id not in (select col1 from tmp);

3.drop table tmp;

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-02-27
mysql中不能这么用。 (等待mysql升级吧)
错误提示就是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)

替换方案:
create table tmp as select min(id) as col1 from blur_article group by title;
delete from blur_article where id not in (select col1 from tmp);
drop table tmp;

已经测试,尽请使用本回答被提问者和网友采纳
第2个回答  2008-09-17
MySql不是很熟,试试:
create table 临时表
select min(id) as id from blur_article group by title;

delete from blur_article where id not in (select id from 临时表);

删除临时表
第3个回答  2008-09-17
你相当于在视图中删除数据,当然不行了
第4个回答  2008-09-17
排除重复可以用select distinct 或者用group by
相似回答