You can’t specify target table for update in FROM clause
其含义是:不能在同一表中查询的数据作为同一表的更新数
单独执行复合查询是正常的,如下:
但是当执行子查询删除命令时,报如下错误
DELETE FROM abpusers WHERE Id IN
(
SELECT u.Id FROM `abpusers` u
left join abpuserroles ur on u.Id=ur.UserId
LEFT JOIN abproles r on ur.RoleId=r.Id
LEFT join base_companyinfo c on u.Id=c.UserId
WHERE r.`Name` in('supplier','demand')
and c.Id is NULL
)
且在其他类型的数据库中并不会出现,解决方法也简单,就是再封装一次查询,让数据库认为你不是查同一表的数据作为同一表的更新数据,如下:
DELETE FROM abpusers WHERE Id IN
(
select Id from (
SELECT u.Id FROM `abpusers` u
left join abpuserroles ur on u.Id=ur.UserId
LEFT JOIN abproles r on ur.RoleId=r.Id
LEFT join base_companyinfo c on u.Id=c.UserId
WHERE r.`Name` in('supplier','demand')
and c.Id is NULL
) a
)
结果如下,已成功影响21行