其他分享
首页 > 其他分享> > You can't specify target table for update in FROM clause

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

作者:互联网

一、错误

报错:You can't specify target table 'p_function' for update in FROM clause
原因:MySQL中不能先select出同一表中的某些值,再update这个表(在同一语句中)

delete from p_function where function_id not in (
select function_id from p_function where parent_id in (select function_id from p_function where parent_id in ('e85563d8-9f54-11ec-a242-00e04c842f1e','00c2e1ad-522f-4cf5-b31d-9424af61bf2a')) union 
select function_id from p_function where parent_id in ('e85563d8-9f54-11ec-a242-00e04c842f1e','00c2e1ad-522f-4cf5-b31d-9424af61bf2a')
)

二、解决

通过使用虚拟表来操作一波

delete from p_function where function_id not in (
	select tmp.function_id from
	(select function_id from p_function where parent_id in (select function_id from p_function where parent_id in ('e85563d8-9f54-11ec-a242-00e04c842f1e','00c2e1ad-522f-4cf5-b31d-9424af61bf2a')) union 
	select function_id from p_function where parent_id in ('e85563d8-9f54-11ec-a242-00e04c842f1e','00c2e1ad-522f-4cf5-b31d-9424af61bf2a')
	) tmp
)

标签:function,target,parent,clause,b31d,table,where,id,select
来源: https://www.cnblogs.com/sglblog/p/16177744.html