问题解决:MySQL中执行sql语句错误 Error Code: 1093

问题场景

mysql执行以下SQL

update test set i_status=5 where s_id in (
	select max(s_id) as id from test where s_title is not null
	group by s_title,length(s_content) having count(*)>1
)

执行报错,提示 : Error Code: 1093. You can't specify target table 'car' for update in FROM clause。本篇博文主要是针对此种情况提出解决方案。

问题环境

软件 版本
mysql 5.7.27

问题原因

如果是子查询操作的表和更新操作是同一张表就会出现这个问题。但是,如果只是查询操作,就不会报错,会正常执行。如下列SQL是不会报错,可以正常执行。

select * from  test where s_id in (
	select max(s_id) as id from test where s_title is not null
	group by s_title,length(s_content) having count(*)>1
)

解决方案

将子查询作为一个子表,进行隔离开就可以解决这个问题。更新之后的SQL如下:

update test  set i_status=5 where s_id in (
	select id from (
		select max(s_id) as id from test where s_title is not null
		group by s_title,length(s_content) having count(*)>1
	) as temp
)

总结

问题顺利解决!!!

随缘求赞

如果我的文章对大家产生了帮忙,可以在文章底部点个赞或者收藏;
如果有好的讨论,可以留言;
如果想继续查看我以后的文章,可以点击关注
可以扫描以下二维码,关注我的公众号:枫夜之求索阁,查看我最新的分享!
在这里插入图片描述
拜拜

猜你喜欢

转载自blog.csdn.net/u013084266/article/details/108146627