Php MySQL查询连接表,其中包括匹配项和非匹配项
以下查询将选民ID与投票意向中的相应选民进行匹配,以查找特定条件Php MySQL查询连接表,其中包括匹配项和非匹配项,php,mysql,join,where,Php,Mysql,Join,Where,以下查询将选民ID与投票意向中的相应选民进行匹配,以查找特定条件 " FROM electors,voting_intention WHERE electors.ID = voting_intention.elector AND electors.telephone > 0 AND electors.postal_vote != 1 AND (mosaic IN ('E1','E2','E3') OR (voting_intention.pledge IN ('C',
" FROM electors,voting_intention
WHERE electors.ID = voting_intention.elector
AND electors.telephone > 0
AND electors.postal_vote != 1
AND (mosaic IN ('E1','E2','E3')
OR (voting_intention.pledge IN ('C','P')
AND voting_intention.election != '2012-07-05')) "
问题在于行和(镶嵌在('E1','E2','E3')
中,因为我们需要获取此信息,即使选民ID和投票意向选民之间不匹配,因为此信息在选民表中
这样做的方法是把这一行放在WHERE等式的第一位。应该这样做
FROM electors e
FULL OUTER JOIN votin_intention i
ON e.ID=i.elector
WHERE electors.telephone > 0
AND electors.postal_vote != 1
AND (mosaic IN ('E1','E2','E3')
OR (voting_intention.pledge IN ('C','P')
AND voting_intention.election != '2012-07-05')) "
您还可以做的是使用左外部联接来拾取所有记录,而不仅仅是那些具有完全联接的记录
WHERE electors.ID(+) = voting_intention.elector
像这样的东西…(未经测试)这个可以吗::
FROM electors,voting_intention WHERE electors.telephone > 0 AND electors.postal_vote != 1 AND (electors.mosaic IN ('E1','E2','E3') OR (electors.ID = voting_intention.elector AND voting_intention.pledge IN ('C','P') AND voting_intention.election_date != '2012-07-05'))
看起来我们今天正在做你的洞项目如果你在做内部连接,那么它是如何影响你的结果的。确保你正确地插入了and和OR。我认为现在的逻辑中有一个模糊的地方,可能会导致你意想不到的结果。这会从选民那里做到吗,在哪里投票ors.telephone>0和electors.postal_vote!=1和(electors.mosaic IN('E1','E2','E3')或(electors.ID=投票意向。选民和投票意向。宣誓IN('C','P')和投票意向。选举意向日期!='2012-07-05'))