我正在对我们的数据库进行更新,并希望更新另一个表中没有现有项目的行。我可以一起加入这些表,但是在分组表以获取行数
时遇到了麻烦UPDATE dpt
SET dpt.active = 0
FROM DEPARTMENT dpt
LEFT JOIN DOCUMENTS doc on dpt.ID = doc.DepartmentID
GROUP BY dpt.ID
HAVING COUNT(doc.ID) = 0
我该怎么办?
答案 0 :(得分:5)
使用:
UPDATE DEPARTMENT
SET active = 0
WHERE NOT EXISTS(SELECT NULL
FROM DOCUMENTS doc
WHERE doc.departmentid = id)
答案 1 :(得分:4)
UPDATE department
SET active = 0
WHERE id NOT IN
(
SELECT departmentId
FROM doc
)