从sql表中删除行,其中在具有多个连接的另一个表中找不到值

时间:2013-09-02 17:28:00

标签: sql join exists delete-row

我有以下查询返回所需的数据。我想要做的是删除该数据,但是当我将Select DQ。*更改为DELETE FROM DQ时,我收到错误消息,指示

  

'...不可更新,因为修改会影响多个基表'

我需要从tbaccess_groupPermission表中删除条目,其中在tbaccess_companyModules表中找不到相应的模块记录。

WITH DQ AS
(
SELECT GP.* FROM tbaccess_groupPermission GP
JOIN tbaccess_groups G ON GP.GroupID = G.ID
WHERE G.CompanyID=6
AND GP.RoleName NOT IN
(
select Distinct(R.RoleName)
FROM tbAccess_Roles R
INNER JOIN tbAccess_CompanyModules C on R.ModuleID = C.ModuleID
WHERE C.CompanyID = 6) 
) 
SELECT * FROM DQ

1 个答案:

答案 0 :(得分:0)

为什么不更改WITH语句以使用IN子句?

这样的东西
WITH DQ AS
(
SELECT GP.* FROM tbaccess_groupPermission GP
WHERE GP.GroupID IN (SELECT G.ID FROM tbaccess_groups G WHERE G.CompanyID=6)
AND GP.RoleName NOT IN
(
select Distinct(R.RoleName)
FROM tbAccess_Roles R
INNER JOIN tbAccess_CompanyModules C on R.ModuleID = C.ModuleID
WHERE C.CompanyID = 6) 
)