ChatGPT解决这个技术问题 Extra ChatGPT

如何在 SQL Server 中使用 INNER JOIN 进行删除?

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

但我得到这个错误:

消息 156,级别 15,状态 1,第 15 行关键字“INNER”附近的语法不正确。

我的代码:

DELETE 
FROM WorkRecord2 
INNER JOIN Employee 
        ON EmployeeRun=EmployeeNo
WHERE Company = '1' 
    AND Date = '2013-05-06'
documentation 中的示例 C 显示了如何将 DELETE 与联接一起使用
示例 C 也使用了一个游标和一堆无关的东西
示例 D 对一个表中的数据使用连接和子查询来删除另一个表中的行可能是正确的。

P
Peter Mortensen

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

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

@bluefeet 您能否为 SQL Server 提供正确的语法以从两个表中删除?
@user2070775 在 SQL Server 中,要从 2 个表中删除,您需要使用 2 个单独的语句。
@user2070775 在 SQL Server 中,可以使用事务和伪表,如stackoverflow.com/questions/783726/…所示
@MathieuRodic 感谢分享。在我的设置中,如果我分别从 2 个表中删除,我真的不知道要从第 2 个表中删除哪些行,所以这将有所帮助:)
@ShahryarSaljoughi 是 WorkRecord2 表的别名。
P
Peter Mortensen

只需在 DELETEFROM 之间添加要删除记录的表名,因为我们必须指定要删除的表。还要删除 ORDER BY 子句,因为在删除记录时没有要排序的内容。

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

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

如果您只打算从第一个表中删除,则此选项适用于 SQL Server。
@matwonk:如果您使用第二个表的名称,您可以从第二个表中删除。例如,使用 DELETE Employee 将从 Employee 表中删除,而不是从 WorkRecord2 表中删除。
@matwonk:这是一个示例:1) Deleting from the first table 2) Deleting from the second table
如果您要从具有自身作为外键的表中删除(并且不使用级联删除),则删除顺序可能很重要。但我一般同意这并不重要......
P
Peter Mortensen

这可能对您有所帮助-

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 ....
)

这是唯一适用于 Sql Server 的答案。只需构建您的查询,例如 select Id from... join ... join 等,然后将其包装为子查询并从(表)中删除(子查询)中的 Id
这是适用于 SQL Server 的众多答案之一。我建议接受的答案是最好的方法。
B
Behrouz Bakhtiari

尝试这个:

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

T
Tony Stark

它应该是:

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

P
Peter Mortensen

SQL Server Management Studio 中,我可以轻松创建 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 语句中看到的所有记录都将被删除。

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

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

P
Peter Mortensen

试试这个查询:

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 只能指定一个表。这对我不起作用。
我相信您可以在 mySQL 中指定要删除的多个表,但不能指定 SQL Server(问题提出)。
P
Peter Mortensen

另一种方法是使用 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


P
Peter Mortensen

你甚至可以做一个子查询。像下面的这段代码:

DELETE FROM users WHERE id IN(
    SELECT user_id FROM Employee WHERE Company = '1' AND Date = '2013-05-06'
)

P
PPJN

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

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

P
Peter Mortensen

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

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

这个问题是针对 SQL Server 的。您不能在 SQL Server 的一个语句中从两个表中删除。我的理解是这可以在 mysql 和 MS Access 中完成。
P
Peter Mortensen

您没有为 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;

The answer from Devart 也是标准 SQL,虽然不完整。它应该看起来更像这样:

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

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

对我来说,各种专有语法答案更难阅读和理解。我想the answer by frans eilering 中最好地描述了这种心态,即编写代码的人不一定关心阅读和维护代码的人。


A
Andy

这是我的 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)

R
Rahul Biswas

使用事务块、表变量和JOIN删除多个表数据。

BEGIN TRANSACTION;

   declare @deletedIds table ( id int );
   
   DELETE w
   output deleted.EmployeeRun into @deletedIds
   FROM WorkRecord2 w
   INNER JOIN Employee e
           ON e.EmployeeNo = w.EmployeeRun
          AND w.Company = 1
          AND w.date = '2013-05-06';

   DELETE e
   FROM Employee as e
   INNER JOIN @deletedIds as d
           ON d.id = e.EmployeeNo;
COMMIT TRANSACTION;

请从网址 https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=43330dda6f1b71b8ec4172a24d5b6921 检查

用临时表和JOIN删除多个表数据。删除后删除临时表。

BEGIN TRANSACTION;

    -- create temporary table
    create table #deletedRecords (employeeId int);
    
    -- INSERT INTO #deletedRecords
    SELECT e.EmployeeNo
    FROM WorkRecord2 w
    INNER JOIN Employee e
           ON e.EmployeeNo = w.EmployeeRun
          AND w.Company = 1
          AND w.date = '2013-05-06';
          
    -- delete from WorkRecord2
    DELETE w
    FROM WorkRecord2 w
    INNER JOIN #deletedRecords d
           ON w.EmployeeRun = d.employeeId;
           
    -- delete from Employee using exists
    DELETE 
    FROM Employee
    WHERE EXISTS (SELECT 1
                  FROM #deletedRecords d
                  WHERE d.employeeId = EmployeeNo);
                  
    -- drop temporary table
    DROP TABLE #deletedRecords;

COMMIT TRANSACTION;

请从网址 https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=d52c6c1ed91669d68fcc6bc91cb32d78 检查

使用 SELECT INTO 创建临时表的替代方法

BEGIN TRANSACTION;

    SELECT  e.EmployeeNo employeeId 
    INTO #deletedRecords
    FROM WorkRecord2 w
    INNER JOIN Employee e
           ON e.EmployeeNo = w.EmployeeRun
          AND w.Company = 1
          AND w.date = '2013-05-06';
          
    -- delete from WorkRecord2
    DELETE w
    FROM WorkRecord2 w
    INNER JOIN #deletedRecords d
           ON w.EmployeeRun = d.employeeId;
           
    -- delete from Employee using exists
    DELETE 
    FROM Employee
    WHERE EXISTS (SELECT 1
                  FROM #deletedRecords d
                  WHERE d.employeeId = EmployeeNo);
                  
    -- drop temporary table
    DROP TABLE #deletedRecords;

COMMIT TRANSACTION;

请从网址 https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=0f02f05616ce5b4dcc8fc67c6cf1e640 检查

使用 JOIN 删除单个表数据

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

请从网址 https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=84a60d1368556a8837281df36579334a 检查

使用 CTE 删除单表数据

WITH cte AS (
     SELECT w.EmployeeRun
     FROM WorkRecord2 w
     WHERE EXISTS (SELECT 1
                   FROM Employee 
                   WHERE EmployeeNo = w.EmployeeRun)
         AND w.Company = 1
         AND w.date = '2013-05-06'
)
DELETE
FROM cte

请从网址 https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=6590007b3c8c2ffad5563bd86606c5b1 检查

在子表中创建外键时使用 ON CASCADE DELETE。如果删除父表数据,则相应的子表数据将被自动删除。