如何使用INNER JOIN与SQL Server删除?

我想在SQL Server 2008中使用INNER JOIN进行删除。

但是我得到这个错误,

Msg 156,Level 15,State 1,Line 15
关键字“INNER”附近的语法不正确。

我的代码:

 DELETE FROM WorkRecord2 INNER JOIN Employee ON EmployeeRun=EmployeeNo WHERE Company = '1' AND Date = '2013-05-06' 

您需要指定要删除的表,这里是一个带有别名的版本:

 DELETE w FROM WorkRecord2 w INNER JOIN Employee e ON EmployeeRun=EmployeeNo WHERE Company = '1' AND Date = '2013-05-06' 

只需在要删除logging的位置添加DELETEFROM之间的表名称,因为我们必须指定要删除的表。 同时删除ORDER BY子句,因为在删除logging时没有任何东西需要sorting。

所以你的最终查询应该是这样的:

  DELETE WorkRecord2 FROM WorkRecord2 INNER JOIN Employee ON EmployeeRun=EmployeeNo WHERE Company = '1' AND Date = '2013-05-06'; 

尝试这个:

 DELETE FROM WorkRecord2 FROM Employee Where EmployeeRun=EmployeeNo And Company = '1' AND Date = '2013-05-06' 

可能这对你有帮助 –

 DELETE FROM dbo.WorkRecord2 WHERE EmployeeRun IN ( SELECT e.EmployeeNo FROM dbo.Employee e WHERE ... ) 

或者试试这个 –

 DELETE FROM dbo.WorkRecord2 WHERE EXISTS( SELECT 1 FROM dbo.Employee e WHERE EmployeeRun = e.EmployeeNo AND .... ) 

它应该是:

 DELETE zpost FROM zpost INNER JOIN zcomment ON (zpost.zpostid = zcomment.zpostid) WHERE zcomment.icomment = "first" 

这个版本应该工作

 DELETE WorkRecord2 FROM WorkRecord2 INNER JOIN Employee ON EmployeeRun=EmployeeNo Where Company = '1' AND Date = '2013-05-06' 

试试这个查询:

 DELETE WorkRecord2, Employee FROM WorkRecord2 INNER JOIN Employee ON (tbl_name.EmployeeRun=tbl_name.EmployeeNo) WHERE tbl_name.Company = '1' AND tbl_name.Date = '2013-05-06'; 
  DELETE a FROM WorkRecord2 a INNER JOIN Employee b ON a.EmployeeRun = b.EmployeeNo Where a.Company = '1' AND a.Date = '2013-05-06' 

在SQL Server Management Studio中,我可以轻松创build一个SELECT查询。

 SELECT Contact.Naam_Contactpersoon, Bedrijf.BedrijfsNaam, Bedrijf.Adres, Bedrijf.Postcode FROM Contact INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf 

我可以执行它,并显示我所有的联系人。

现在将SELECT更改为DELETE:

 DELETE Contact FROM Contact INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf 

您在SELECT语句中看到的所有logging都将被删除。

你甚至可以用同样的程序创build一个更难的内连接,例如:

 DELETE FROM Contact INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf INNER JOIN LoginBedrijf ON Bedrijf.IDLoginBedrijf = LoginBedrijf.IDLoginBedrijf 

这是我的SQL Server版本

 DECLARE @ProfileId table(Id bigint) DELETE FROM AspNetUsers OUTPUT deleted.ProfileId INTO @ProfileId WHERE Email = @email DELETE FROM UserProfiles WHERE Id = (Select Id FROM @ProfileId) 

您不指定CompanyDate的表格,您可能想要解决该问题。

使用MERGE标准SQL:

 MERGE WorkRecord2 T USING Employee S ON T.EmployeeRun = S.EmployeeNo AND Company = '1' AND Date = '2013-05-06' WHEN MATCHED THEN DELETE; 

@Devart的答案也是Standard SQL,虽然不完整,应该看起来更像这样:

 DELETE FROM WorkRecord2 WHERE EXISTS ( SELECT * FROM Employee S WHERE S.EmployeeNo = WorkRecord2.EmployeeRun AND Company = '1' AND Date = '2013-05-06' ); 

关于上述情况需要注意的重要一点是,删除是针对单个表,如第二个示例中通过要求标量子查询所强制的。

对于我来说,各种专有的语法答案很难阅读和理解。 我想最好在@frans eilering的回答中描述一下,即编写代码的人不一定会关心读者和维护代码的人。

另一种使用CTE

 ;WITH cte AS (SELECT * FROM workrecord2 w WHERE EXISTS (SELECT 1 FROM employee e WHERE employeerun = employeeno AND company = '1' AND date = '2013-05-06')) DELETE FROM cte 

注意:当你想delete时,我们不能在CTE里面使用JOIN

以下是我目前用于删除甚至更新的内容:

 DELETE FROM w FROM WorkRecord2 w, Employee e WHERE w.EmployeeRun = e.EmployeeNo AND w.Company = '1' AND w.Date = '2013-05-06' 

这是一个简单的查询,一次删除两个表中的logging。

 DELETE table1.* , table2.* FROM table1 INNER JOIN table2 ON table1.id= table2.id where table1.id ='given_id'