How to get a list of mutual friends
Hi IM trying to find out how i can get mutual friends
im currently having problems thinking this out.
I have a table called "users" and this is how it looks
id | name
-----------
1 Kenny
2 Jack
3 Jimmy
4 Chris
5 Meg
6 Jake
7 Micheal
8 Dude
I have a table called "friendship" and this is how it looks
user_a | user_b
----------------
4 开发者_如何学Go 1
7 5
8 1
2 4
2 1
5 2
1 6
1 7
user_a sends user_b a request to become friends and... BAM there are friends. Now if im user 2, and i go to user 1s friend list, i want to see what friends we have in common. What is the correct sql to do that?
Assuming that it is not possible for User 1 to be friends with User 7 twice, (meaning there can not be a row with user_a = 1, user_b=7
and another row user_a = 7, user_b = 1
).
SELECT IF(user_a = 1 OR user_a = 2, user_b, user_a) friend
FROM friendship
WHERE (user_a = 1 OR user_a = 2) OR (user_b = 1 OR user_b = 2)
GROUP BY 1
HAVING COUNT(*) > 1
精彩评论