SELECT DISTINCT
two.idUserFriend
FROM userFriends one
JOIN userFriends two ON one.idUserFriend = two.idUser
WHERE NOT EXISTS (
SELECT *
FROM userFriends nx
WHERE nx.idUser = one.idUser
AND nx.idUserFriend = two.idUserFriend
)
AND one.idUser = 1
;
同样可以通过“NOT IN”构造来完成:
SELECT DISTINCT
two.idUserFriend
FROM userFriends one
JOIN userFriends two ON one.idUserFriend = two.idUser
WHERE two.idUserFriend NOT IN (
SELECT nx.idUserFriend
FROM userFriends nx
WHERE nx.idUser = one.idUser
)
AND one.idUser = 1
;
还有“EXCEPT”版本:
SELECT DISTINCT
two.idUserFriend
FROM userFriends one
JOIN userFriends two ON one.idUserFriend = two.idUser
WHERE one.idUser = 1
EXCEPT (
SELECT nx.idUserFriend
FROM userFriends nx
WHERE nx.idUser = 1
)
;