我想从表中删除一些数据。我想删除内部联接中不包含的数据

到目前为止我的查询:

 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


在此先感谢您提供的所有帮助:)!

最佳答案

您可以使用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

关于sql - 使用内部联接删除,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23344024/

10-11 01:39