我想从表中删除一些数据。我想删除内部联接中未包含的数据
到目前为止我的查询:
DELETE *how do you say delete what wasn't included in the inner join?*
from dbo.Cloudpie c
inner join dbo.cake p
on c.cakeid = p.cakeid
where cakeid > 1
提前感谢您提供的所有帮助:)!
答案 0 :(得分:5)
您可以使用LEFT OUTER JOIN
:
DELETE c
FROM dbo.Cloudpie c
LEFT OUTER JOIN dbo.cake p
ON c.cakeid = p.cakeid
WHERE p.cakeid IS NULL AND c.cakeid > 1
使用NOT EXISTS
甚至更简单,更不容易出错(在可空列上):
DELETE c
FROM dbo.Cloudpie c
WHERE NOT EXISTS( SELECT 1 FROM dbo.cake p
WHERE p.cakeid = c.cakeid )
AND c.cakeid > 1
值得一读:http://www.sqlperformance.com/2012/12/t-sql-queries/left-anti-semi-join