2

下面的代码用于查找没有邮寄投票、有电话号码、不拥有某些代码的选民,这些代码列在另一个表中,称为具有某些承诺的投票意向,或者根本没有列在该表中。

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 万条记录,是数据库中记录的数倍。显然出了点问题,但我看不到我的错误。

4

2 回答 2

1

看起来你缺少选举者和投票意图之间的连接定义

我希望看到类似的东西:

FROM electors e 
INNER JOIN voting_intention v 
    ON v.elector_id = e.id

显然,每个表都用正确的键替换了键。

于 2012-06-28T13:30:52.353 回答
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)
于 2012-06-28T13:32:49.797 回答