表:成员
fields:
member_id - unique primary id
voter_id - the member_id of another record
John Doe - member_id = 1, voter_id = 0
ABC Inc. - member_id = 2, voter_id = 1 (John Doe is the voter)
我需要提取所有记录,其中member_id永远不会出现在voter_id字段中。在这种情况下,我需要它来查找member_id 2,但不是member_id 1。
谢谢你们。我看了,但没有在这里找到答案。
答案 0 :(得分:1)
有很多方法可以做到这一点......这里有一些。 子选择
SELECT *
FROM member
WHERE member_ID not in (select voter_ID from member)
JOIN
SELECT *
FROM member ma
LEFT join member mb on mb.voter_Id = ma.member_Id and Mb.meber_ID is null
EXISTS
SELECT Member_ID, Voter_ID
FROM member MA
WHERE not exists (Select 1 from member MA where MA.Member_ID = MB.Voter_ID)
选择哪个取决于很多东西,但这里只有一个opinion
答案 1 :(得分:0)
尝试缺少匹配自我加入:
SELECT m1.member_id
FROM member m1
LEFT JOIN member m2 ON m2.voter_id = m1.member_id
AND m2.member_id IS NULL
答案 2 :(得分:0)
SELECT * from member WHERE member_id NOT IN (SELECT voter_id FROM member);