为什么两个oposit查询返回相同的数据?

时间:2014-08-01 09:08:17

标签: sqlite

那些查询:

SELECT * FROM eloquaActual as ea 
WHERE exists (SELECT * FROM eloquaActual as eff 
WHERE (eff.Email_Address != ea.Email_Address))

SELECT * FROM eloquaActual as ea 
WHERE exists (SELECT * FROM eloquaActual as eff 
WHERE (eff.Email_Address = ea.Email_Address))

返回完全相同的数据。另请注意,当我将数据导入sqlite并且电子邮件地址列是唯一的时,它会向我显示错误。我试图导入不是唯一的电子邮件。然后我试图在电子邮件列中找到任何重复项。当我执行上述查询时,它返回了所有数据。这意味着所有行都是重复的。

然后我选择了电子邮件地址=任何电子邮件,只找到1条记录... wtf?

表格结构:

CREATE TABLE "eloquaActual" ("Email_Address" VARCHAR, "Prizes_Oddset_7" VARCHAR)

之前我Email_Address唯一,但发生了错误。

1 个答案:

答案 0 :(得分:2)

exists (SELECT * FROM eloquaActual as eff 
        WHERE (eff.Email_Address != ea.Email_Address))

检查是否有任何电子邮件与ea中当前行的电子邮件不同。 如果表格包含至少两封电子邮件,则会发生这种情况。

exists (SELECT * FROM eloquaActual as eff 
        WHERE (eff.Email_Address = ea.Email_Address))

检查是否有任何电子邮件与当前行的电子邮件相同。 这将永远发生,因为该行将匹配自己。


要查找重复项,您可以使用相同的电子邮件检查某些行,但不同的 ID:

SELECT *
FROM eloquaActual AS ea
WHERE EXISTS (SELECT *
              FROM eloquaActual AS eff
              WHERE eff.Email_Address = ea.Email_Address
                AND eff.rowid != ea.rowid)

使用GROUP BY会更容易:

SELECT Email_Address,
       COUNT(*)
FROM eloquaActual
GROUP BY Email_Address
HAVING COUNT(*) >= 2