Reputation: 13
I have a table which maps friendships between two user ids. The table has two columns userid1 and userid2. If userid1 adds userid2 as a friend then that will be one row. If userid2 adds userid1 as a friend then that will be another row added to the table This is similar to how Facebook needs both users to add each other as friends before a friendship is established.
How do I query the table so I find out all the userid pairs that have added each other as friends?
Upvotes: 1
Views: 1515
Reputation: 4401
You could join back to the same table. Something like the below;
SELECT a.userid1, a.userid2
FROM friendship a
INNER JOIN friendship b
ON a.userId1 = b.userId2 AND b.userId1 = a.userId2
Upvotes: 2