我必须用很多孤立的条目清理数据库,在这种情况下我有一个表3表
最后两个表引用了“电子邮件”表,因此该表应包含26254 + 5782(32036)个条目。
我构建了一个查询来选择'Email'表中所有未在UserHasEmail和ServiceHasEmail表中引用的条目:
SELECT * FROM Email e
WHERE e.EML_Id NOT IN (SELECT EML_Id FROM ServiceHasEmail)
AND e.EML_Id NOT IN (SELECT EML_Id FROM UserHasEmail)
但此查询返回40383条目代替37493(69529 - (26254 + 5782))
我在这里缺少什么?
答案 0 :(得分:1)
这可能是因为
您可以验证:
select count(distinct email) from Email
select count(distinct email) from ServiceHasEmail
select count(distinct email) from UserHasEmail
和
select count(distinct Email)
from
(select Email from ServiceHasEmail
union all
select Email from UserHasEmail
)
您的查询应该是
SELECT count(distinct Email)
FROM Email e
WHERE e.EML_Id NOT IN (SELECT EML_Id FROM ServiceHasEmail)
AND e.EML_Id NOT IN (SELECT EML_Id FROM UserHasEmail)
答案 1 :(得分:0)
看起来某些ServiceHasEmail和UserHasEmail引用相同的电子邮件。
答案 2 :(得分:0)
EML_Id
和ServiceHasEmail
中都有UserHasEmail
个。
我想你已经完成了2,890个。请尝试
SELECT * FROM `ServiceHasEmail` INNER JOIN `UserHasEmail` USING(`EML_Id`)
验证这一点。
答案 3 :(得分:0)
您可以使用以下查询:
SELECT * FROM Email e 在哪里e.EML_Id NOT IN(从UserHasEmail中选择EML_Id UNION SELECT EML_Id来自UserHasEmail)