我试图从选民表中删除所有选民,他们没有登记为民主人士或共和党人,只投票一次.我有一个包含三个表,congress_members,选民和投票的数据库,并且必须与选民加入投票才能删除正确的数据.
SELECT voters.* FROM voters JOIN votes ON voters.id = votes.voter_id WHERE party = 'green' OR party = 'na' OR party = 'independent' GROUP BY votes.voter_id HAVING COUNT(*) = 1;
解决方法
您可以将此短语用作带有where子句的删除:
delete from voters where votes.party not in ('democrat','republican') and voters.id in (select id from votes group by id having count(*) = 1);