You can't specify target table for update in FROM clause
阿新 • • 發佈:2022-04-22
一、錯誤
報錯: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 )