SQL Server如何使用内部联接删除?

How to Delete using INNER JOIN with SQL Server?

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

但我得到这个错误:

Msg 156, Level 15, State 1, Line 15
Incorrect syntax near the keyword 'INNER'.

我的代码:

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


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

1
2
3
4
5
DELETE w
FROM WorkRecord2 w
INNER JOIN Employee e
  ON EmployeeRun=EmployeeNo
WHERE Company = '1' AND DATE = '2013-05-06'


只需在DELETEFROM之间添加表名,因为我们必须指定要删除的表,所以要从中删除记录。同时删除ORDER BY子句,因为删除记录时没有要订购的内容。

所以您的最终查询应该如下所示:

1
2
3
4
5
6
    DELETE WorkRecord2
      FROM WorkRecord2
INNER JOIN Employee
        ON EmployeeRun=EmployeeNo
     WHERE Company = '1'
       AND DATE = '2013-05-06';


试试这个:

1
2
3
4
5
DELETE FROM WorkRecord2
       FROM Employee
WHERE EmployeeRun=EmployeeNo
      AND Company = '1'
      AND DATE = '2013-05-06'


可能这对你有帮助-

1
2
3
4
5
6
DELETE FROM dbo.WorkRecord2
WHERE EmployeeRun IN (
    SELECT e.EmployeeNo
    FROM dbo.Employee e
    WHERE ...
)

或者试试这个——

1
2
3
4
5
6
7
DELETE FROM dbo.WorkRecord2
WHERE EXISTS(
    SELECT 1
    FROM dbo.Employee e
    WHERE EmployeeRun = e.EmployeeNo
        AND ....
)


应该是:

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

这个版本应该有效

1
2
3
4
DELETE WorkRecord2
FROM WorkRecord2
INNER JOIN Employee ON EmployeeRun=EmployeeNo
WHERE Company = '1' AND DATE = '2013-05-06'


尝试此查询:

1
2
3
4
5
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';


在SQL Server Management Studio中,我可以轻松地创建一个选择查询。

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

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

现在将选择更改为删除:

1
2
3
DELETE Contact
FROM Contact
INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf

您在select语句中看到的所有记录都将被删除。

甚至可以使用相同的过程创建更困难的内部联接,例如:

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

1
2
3
4
5
 DELETE a FROM WorkRecord2 a
       INNER JOIN Employee b
       ON a.EmployeeRun = b.EmployeeNo
       WHERE a.Company = '1'
       AND a.Date = '2013-05-06'


试试这个,可能会有帮助

1
2
3
4
5
6
 DELETE WorkRecord2
          FROM WorkRecord2
    INNER JOIN Employee
            ON EmployeeRun=EmployeeNo
         WHERE Company = '1'
           AND DATE = '2013-05-06';


另一种使用CTE的方法。

1
2
3
4
5
6
7
8
9
;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


这是一次从两个表中删除记录的简单查询。

1
2
3
4
DELETE table1.* ,
       table2.*
FROM table1
INNER JOIN table2 ON table1.id= table2.id WHERE table1.id ='given_id'


您没有为CompanyDate指定表,您可能需要修复它。

使用MERGE的标准SQL:

1
2
3
4
5
6
MERGE WorkRecord2 T
   USING Employee S
      ON T.EmployeeRun = S.EmployeeNo
         AND Company = '1'
         AND DATE = '2013-05-06'
WHEN MATCHED THEN DELETE;

@devart的答案也是标准的SQL,虽然不完整,但应该更像这样:

1
2
3
4
5
6
7
DELETE
  FROM WorkRecord2
  WHERE EXISTS ( SELECT *
                   FROM Employee S
                  WHERE S.EmployeeNo = WorkRecord2.EmployeeRun
                        AND Company = '1'
                        AND DATE = '2013-05-06' );

关于上述内容,需要注意的重要一点是,显然删除的目标是单个表,正如第二个示例中通过要求标量子查询而强制执行的那样。

对我来说,各种专有的语法答案很难阅读和理解。我想@frans eilering最好在答案中描述他们的心态,也就是说,编写代码的人并不一定关心阅读和维护代码的人。


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

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

这是我的SQL Server版本

1
2
3
4
5
6
7
8
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)