0

表:成员

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。

多谢你们。我看了但没有在这里找到答案。

4

3 回答 3

1

有很多方法可以做到这一点..这里有一些。子选择

SELECT * 
FROM  member 
WHERE member_ID not in (select voter_ID from member)

加入

SELECT  * 
FROM member ma
LEFT join member mb on mb.voter_Id = ma.member_Id and Mb.meber_ID is null

存在

SELECT Member_ID, Voter_ID 
FROM member MA 
WHERE not exists (Select 1 from member MA where MA.Member_ID = MB.Voter_ID)

至于选择哪个取决于很多事情,但这是一种意见

于 2013-05-22T19:52:10.767 回答
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
于 2013-05-22T19:44:50.627 回答
0
SELECT * from member WHERE member_id NOT IN (SELECT voter_id FROM member);
于 2013-05-22T19:44:55.577 回答