user3072519
user3072519

Reputation: 13

MySQL query that checks if two user ids are friends

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

Answers (1)

Christoph
Christoph

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

Related Questions