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

来源:百度知道 编辑:UC知道 时间:2024/06/04 18:05:30
在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.创建一张临时表,将要删除的条件自动存入临时表中:

So if you think you can ,you can! If you think you can't ,you can't. [天堂2单机]Can't connect to MySQL server MySQL 返回: Can't connect to MySQL server on 'localhost' (10061) can you......? you can ??? CAN YOU??? if you can,you can. can't love without you Can't smile without you Who's help me? Can you?