0

我必须连接两个表并从一个表中删除第二个表中不存在的行。我正在尝试下面的查询。我应该只删除 7 条记录,但它会删除整个 PERSONS 表。

DELETE PERSONS.*
FROM PERSONS
WHERE EXISTS (
    SELECT PERSONS.[IR NUMBER],
      PERSONS.[Last Name],
      PERSONS.[First Name],
      PERSONS.DOB,
      PERSONS.[Name Type]
    FROM 1 tblPERSON
    RIGHT JOIN PERSONS
      ON (
             iif(isnull([1tblPERSON].DOB) = true, "01/01/9999", [1tblPERSON].DOB) = 
             iif(isnull(PERSONS.DOB) = true, "01/01/9999", PERSONS.DOB)
         )
        AND ([1tblPERSON].FIRSTNAME = PERSONS.[First Name])
        AND ([1tblPERSON].LASTNAME = PERSONS.[Last Name])
        AND ([1tblPERSON].NAMETYPELIT = PERSONS.[Name Type])
        AND ([1tblPERSON].INCINMBR = PERSONS.[IR Number])
    WHERE (
        (
          (PERSONS.[IR Number]) IN (
            SELECT [QryIncinmbrsForDelete] ! [INCINMBR]
            FROM [QryIncinmbrsForDelete]
            )
          )
        AND (
          (PERSONS.[Last Name]) NOT IN (
            "UNKNOWN",
            "UNK",
            "UKNOWN"
            )
          )
        AND ((IsNull([1tblPERSON].[INCINMBR])) = True)
        )
    ) = true;

编辑:

在此示例中... 1, DOE, JOHN, 1/1/1960, Cust 将被删除

1人:

INCINMBR Last Name First Name DOB      NameType 
1        DOE       JON        1/1/1960 Cust 
1        SMITH     JOHN       2/2/1965 Emp 
1        JONES     JAY        3/3/1965 Cust 

人员:

INCINMBR Last Name First Name DOB      NameType 
1        DOE       JON        1/1/1960 Cust 
1        SMITH     JOHN       2/2/1965 Emp 
1        JONES     JAY        3/3/1965 Cust 
1        DOE       JOHN       1/1/1960 Cust 
4

1 回答 1

0

问题是您没有告诉 delete 语句要从 PERSONS 中删除什么,而是告诉它删除所有内容,如果您的子查询返回任何结果(据我了解,您希望它返回 7? )

如果您可以提供有关密钥的更多信息,那么我可以为您提供完整的解决方案。

于 2013-07-25T17:59:21.210 回答