以下代码应该找到没有邮政投票的选民,确实有一个没有某些代码的电话号码被列在另一个表格中,这些表格被称为具有某些承诺的投票意图,或者没有在该表中列出所有
FROM electors,voting_intention
WHERE
electors.telephone > 0
AND electors.postal_vote != 1
AND (
electors.mosaic NOT IN ('E1','E2','E3')
OR (
electors.ID = voting_intention.elector
AND voting_intention.pledge IN ('U','W','X')
)
OR electors.ID != voting_intention.elector
)
目前它产生的记录超过200万条记录,是数据库中的数字的数倍。显然有些事情是错的,但我看不出我的错误。
答案 0 :(得分:1)
看起来你错过了electors和voting_intention
之间联接的定义我希望看到类似的东西:
FROM electors e
INNER JOIN voting_intention v
ON v.elector_id = e.id
显然,每个表都用正确的键替换了键。
答案 1 :(得分:1)
FROM electors e
LEFT JOIN voting_intention v1 ON e.ID = v1.elector AND v1.pledge IN ('U','W','X')
LEFT JOIN voting_intention v2 ON e.ID = v2.elector
WHERE
e.telephone > 0
AND e.postal_vote != 1 /* do not have a postal vote?? */
AND (e.mosaic NOT IN ('E1','E2','E3')
OR v1.elector IS NOT NULL
OR v2.elector IS NULL)