How to list all pending friend requests for a given user ID? I have a friendship table like this



A and B are friends if there are (A,B) and (B,A) rows. For example, uid 132 &amp; 67 are friends indicated by (132,67) on 2nd row and (67,132) on ninth row. Rows 4 - 8 are pending requests to uid 132.

My question is how to list all pending requests to uid X? I tried this (X=132):

SELECT a.uid1, a.uid2 FROM tbl_friendship a LEFT JOIN tbl_friendship b ON a.uid1=b.uid2 AND a.uid2=b.uid2 WHERE a.uid1 IS NULL AND b.uid2 =132


Still not right because the result is empty. The expected result is something like:

83, 90, 93, 94, 97


How to fix the query?
how to list all pending requests to uid X? I tried this (X=132)


SELECT GREATEST(uid1, uid2), LEAST(uid1, uid2)
FROM tbl_friendship 
WHERE 132 IN (uid1, uid2)
GROUP BY 1, 2
HAVING COUNT(*) = 1



  I'm only interested to find everyone who send friend request to 132. – anta40


SELECT t1.uid2
FROM tbl_friendship t1
WHERE t1.uid2 = 132
  AND NOT EXISTS ( SELECT NULL
                   FROM tbl_friendship t2
                   WHERE (t1.uid1, t1.uid2) = (t2.uid2, t2.uid1) );