在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;