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
)
posted @ 2022-04-22 10:24  爱吃糖的橘猫  阅读(350)  评论(0编辑  收藏  举报