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
)
;