Mj1992
Mj1992

Reputation: 3504

MYSQL Retrieve mutual friends for several users

I am creating a small social network in which when a user posts anything.I will get ids of his friends to whom he referred the post.Now the problem is that the users who will be referred can be between 1 or 10. So now suppose that a user posted a status and referred 6 friends.

Now i need to find mutual friends between the user who posted the status and the people referred one by one.Meaning that suppose user who posted the status has id 1 and the people who are referred have 2,3,4,5,6,7.

So what the query will do it will find mutual friends between 1 and 2 then 1 and 3 then 1 and 4 and so on till 7.So at the end i will be getting a result containing the mutual friends between the user who posted and the people who are referred(or tagged we can say).

I've two tables User and Friends

 User Table:                                Friends Table:
--------------------------                 -----------------------------------
 UserID    Name                            friendsid     friendname     userid
--------------------------                 -----------------------------------
 1097517748         User1                       3          friend1     536772587                     
 100003052455644    User2                       8          friend2     100003052455644
 536772587          User3                       8          friend3     1097517748
 4                  User4                       3          friend4     100003052455644

Now the friends table can contain different people who does or does not exist in the user table.In this example user 2 and 3 has friend with id 8 in common.I've got my guy but that guy id i.e 8 must be present in the user table also.This is only between userid 2 and 3. I will be getting several userid and i've find mutual friends of all the friends that are referred with the guy who has posted the status.

I hope I've cleared it.If I get the idea for the query I can generate it dynamically using php for different number of referred friends.

So far I've written this query.

SELECT count(f1.friendsid) FROM friends AS f1
INNER JOIN friends AS f2 ON f1.friendsid = f2.friendsid
WHERE f1.userid = 100003052455644
AND f2.userid = 1097517748

But this is only bringing me mutual friends between two user.When I add a third guy lets say f3 and compare it with f1 it does not bring any result and another problem with this query is that it is not verifying that the mutual friend id exists in the user table or not.After adding the 3rd guy.

SELECT count(f1.friendsid) FROM friends AS f1
INNER JOIN friends AS f2 ON f1.friendsid = f2.friendsid
INNER JOIN friends AS f3 ON f1.friendsid = f3.friendsid
WHERE f1.userid = 100003052455644
AND f2.userid = 1097517748
AND f3.userid = 536772587

This query does not bring any results.So I wanted to create a query that does all this.

EDIT:

Actually I am using facebook connect and I save all the users friends in my database when users login, the user table will contain only those users who are registered on my website.

Upvotes: 0

Views: 567

Answers (1)

Mj1992
Mj1992

Reputation: 3504

I finally figured it out. The query was

SELECT DISTINCT(friendsid) FROM (
SELECT DISTINCT(f1.friendsid) FROM friends AS f1 
INNER JOIN friends AS f2 ON f1.friendsid=f2.friendsid 
and f1.userid = 100003052455644 and f2.userid =1097517748 
INNER JOIN user ON  f2.friendsid = user.userid and f1.friendsid = user.userid 
UNION ALL
SELECT  (f1.friendsid) FROM friends AS f1 
INNER JOIN friends AS f2 ON f1.friendsid=f2.friendsid 
and f1.userid = 100003052455644  and f2.userid =536772587
INNER JOIN user ON  f1.friendsid = user.userid and f2.friendsid = user.userid 
UNION ALL
SELECT DISTINCT(f1.friendsid) FROM friends AS f1 
INNER JOIN friends AS f2 ON f1.friendsid=f2.friendsid 
and f1.userid = 100003052455644  and f2.userid =694250830
INNER JOIN user ON  f1.friendsid = user.userid and f2.friendsid = user.userid 
) a

This query will find the id's of all the mutual friends between the user who posted the status and the users whom he refered.

The user with id 100003052455644 is the person who posted status and all the others are his friends whom he referred.The DISTINCT will make sure that the id's are unique and not repeated.This is for 3 persons he referred.We can add the UNION ALL part dynamically setting the id's and this will help us create a dynamic query for retrieving mutual friends for different number of referred people.

Upvotes: 1

Related Questions