PHP mySQL JOIN确定不相等的记录

时间:2012-06-29 08:44:05

标签: php mysql

以下查询旨在查找与另一个表中的相应选民ID不相等的记录。

SELECT *
  FROM electors,voting_intention
  WHERE  electors.ID != voting_intention.elector

这应返回此FIDDLE http://sqlfiddle.com/#!3/0a4b1/10中的1条记录,但会返回多条记录并重复。显然我错过了一些东西。什么?

2 个答案:

答案 0 :(得分:1)

如果您希望让所有选民没有任何相应的投票意向。记录,请尝试以下。我假设你想要那个选民的所有领域。如果没有,您需要选择感兴趣的列。

select * from 
electors where id not in 
(select elector from voting_intention)

答案 1 :(得分:1)

SELECT *
  FROM voting_intention
  RIGHT JOIN electors ON electors.ID = voting_intention.elector
  WHERE  voting_intention.elector IS null