Sql 从没有引用ID的表中删除项

Sql 从没有引用ID的表中删除项,sql,Sql,我需要删除所有没有分配许可证的贡献者 Table : Columns Contributors : [id, Name,...] Licenses : [id, ContributorId, Name, ...] 像这样的 DELETE FROM Contributors WHERE License.ContributorId != Contributor.Id 我相信,如果您使用的是SQL Server,这将起作用: DELETE Contributors FRO

我需要删除所有没有分配许可证的贡献者

Table        : Columns

Contributors : [id, Name,...]
Licenses     : [id, ContributorId, Name, ...]
像这样的

DELETE FROM Contributors
WHERE
License.ContributorId != Contributor.Id

我相信,如果您使用的是SQL Server,这将起作用:

DELETE Contributors 
FROM Contributors as c
LEFT JOIN License as l
ON c.id = l.ContributorID
WHERE l.id is null
在实际执行删除之前要做的一个好测试是将
delete Contributors
行替换为
SELECT*
。这将显示所有将要删除的记录,因此这是一个很好的健全性检查

因此,您的理智检查如下所示:

SELECT *
FROM Contributors as c
LEFT JOIN License as l
ON c.id = l.ContributorID
WHERE l.id is null

您可能希望在测试您试图执行的任何
DELETE
命令之前备份数据库<代码>删除不是您要多次尝试的命令。;-)ORM解决方案是你的朋友。我无法得到它。请删除任何内容。。。它总是说超时过期了。。。奇怪的是,当我尝试“理智检查”时,它会返回很多行…:显示您试图删除的多条记录?您从哪里运行查询?
DELETE FROM Contributors
WHERE NOT EXISTS (
  SELECT *
  FROM License
  WHERE License.ContributorId = Contributors.Id)
 DELETE FROM Contributors
    WHERE Contributors.Id NOT IN 
    (SELECT DISTINCT License.ContributorId FROM License)