Merry christmas everyone,
I Know my way around SQL but I’m having a hard time figuring this one out.
First here are my tables (examples)
User
id
name
friend
from //userid
to //userid
If user 1 is friend with user 10
then you a row with 1,10.
User 1 cannot be friend with user 10 if user 10 is not friend with user 1
so you have
1,10
10,1
It may look weird but I need those two rows per relations.
Now I’m trying to make a query to select the users that have the most mutual friend with a given user.
For example User 1 is friend with user 10,9 and 7 and user 8 is friend with 10,9 and 7 too ,I want to suggest user 1 to invite him (like facebook).
I want to get like the 10 first people with the most mutual friend.
The output would be like
User,NumOfMutualFriends
I dont know if that can be done in a single query ?
Thanks in advance for any help.
EDIT1:
This is just a general idea. Note that the count will be inaccurate, there will be duplicates. You’d have to play with some real data to tweak the query to eliminate all the duplicates (or find a real DBA)
EDIT2:
Also I suspect that this is probably not realistic as you are essentially doing a join of with at least n^4 results for the number of users. Which will get pretty bad even if you have 100 of them. So I’m pretty sure Facebook is doing something interesting. No idea what.