我的带有 WHERE EXISTS 的 UPDATE 语句不限于 SELECT 语句结果

My UPDATE statement with WHERE EXISTS does not limit to the SELECT statement results

我在临时 table 中有数据,我正在检查另外两个 table 中的重复项。我想为找到的所有重复项在临时 table (SET DupFlag = TRUE) 上设置一个标志。我的 SELECT 语句运行良好,仅返回我输入用于测试的 48 个重复项。但是当我添加 UPDATE 和 WHERE EXISTS 时,idTempImport2 中的每条记录都设置为 TRUE,而不仅仅是从 SELECT 语句返回的 48 条记录。我的语法错了吗?它看起来像这样:

UPDATE idTempImport2 as tmp2 SET DupFlag = TRUE
WHERE EXISTS
(SELECT * FROM idTempImport2 tmp2
LEFT JOIN (SELECT im.idDate, im.UserID, im.ActionID, im.IsHC, idn.Epic1, idn.Epic2 
      FROM idMain AS im 
      INNER JOIN idNotes AS idn ON im.aID = idn.MainID 
     WHERE idDate BETWEEN "2017-01-02" AND "2017-01-31")  AS qry 
ON qry.idDate = tmp2.idDate AND qry.UserID = tmp2.UserID AND qry.ActionID = tmp2.ActionID AND qry.Epic1 = clng(tmp2.Epic1) AND qry.Epic2 = clng(tmp2.Epic2)
WHERE qry.Epic1 <> NULL);

我认为最终的问题是你想要一个相关的子查询。正如所写,子查询与外部查询没有联系,因此很可能至少有一行满足条件。所以,一切都会更新。

我想你打算:

UPDATE idTempImport2 as tmp2
    SET DupFlag = TRUE
WHERE EXISTS (SELECT im.idDate, im.UserID, im.ActionID, im.IsHC, idn.Epic1, idn.Epic2 
              FROM idMain AS im INNER JOIN
                   idNotes AS idn
                   ON im.aID = idn.MainID 
              WHERE idDate BETWEEN "2017-01-02" AND "2017-01-31" AND
                    im.idDate = tmp2.idDate AND im.UserID = tmp2.UserID AND
                    im.ActionID = tmp2.ActionID AND
                    ?.Epic1 = clng(tmp2.Epic1) AND ?.Epic2 = clng(tmp2.Epic2) 
             );