0

I know you can't get friends of friends, but can you get mutual friends using some query, in a computationally efficient manner?

Could I even iterate through my friends to see which of them is friends with a target? Is there any good way to get Mutual friends?

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
JoshDG
  • 3,871
  • 10
  • 51
  • 85

3 Answers3

3

You can do it like this:

$facebook = new Facebook(array(
  'appId'  => FB_ID,
  'secret' => FB_APP_SECRET,
  'cookie' => true,
));


$fql = "SELECT id FROM profile WHERE id IN (SELECT uid2 FROM friend WHERE uid1=me())";

$response = $facebook->api(array(
'method' => 'fql.query',
'query' =>$fql,
));

This will return the id of the mutual friends. Hope this helps you :)

EDIT:

If you want to get the mutual friends between you and id=13245 then you can do like:

$facebook = new Facebook(array(
  'appId'  => FB_ID,
  'secret' => FB_APP_SECRET,
  'cookie' => true,
));

$fql = "SELECT uid1, uid2 FROM friend WHERE uid1 IN (SELECT uid2 FROM friend WHERE uid1=me() AND uid2 = '13245' ) AND uid2 IN (SELECT uid2 FROM friend WHERE uid1=me())";

$response = $facebook->api(array(
'method' => 'fql.query',
'query' =>$fql,
));

It will return all mutual friends id.

Hope this gives you what you want.

Sabari
  • 6,205
  • 1
  • 27
  • 36
  • I don't see how this works. Where do I put in the second user? Say I want mutual friends between me() and id=13245 how do I structure that query? – JoshDG Jan 31 '12 at 05:37
  • What are you getting now.. Are you getting array of friends ids – Sabari Jan 31 '12 at 06:12
  • @JoshDG, That way you can only get mutual friends if both users connected with your application. – Juicy Scripter Jan 31 '12 at 07:35
  • @Juicy Scripter.You can get the mutual friends even if they are not connectes using my query. – Sabari Jan 31 '12 at 08:15
  • @Sabari - You use uid1 and uid2 too many times to comprehend what is happening there. Also, it seems like this would perform better with stashing the results of the inner query via multi query API. – Peter H. Boling Mar 18 '13 at 22:07
1

This might be old, i needed to calculate mutual friends between user X and me and found that @Sabari answer useful, except it had a mistake (unnecessary inner query)

SELECT uid2 FROM friend WHERE uid1=me() AND uid2 = '13245' )

This inner query will give one result ( 13245 ) as the friend table has two columns (both compose the primary key of the table) and the where clause is putting a condition on both columns.

so the whole query after removing this inner query should be:

SELECT uid1, uid2 FROM friend where uid1='13245' and uid2 in (SELECT uid2 FROM friend where uid1=me())

This query calculates a list of all '13245' friends ( where uid1='13245' ) who are also my friends (the inner query)

here is my code to get the list of mutual friends in python

def get_friends_ids(id):
    query = "select uid1,uid2 from friend where uid1=\"" + str(id) + "\" and uid2 in (select uid2 from friend where uid1=me())"
    query = urllib2.quote(query)
    url = "https://graph.facebook.com/fql?q=" + query + "&access_token=" + access_token
    data = urllib2.urlopen(url).read()
    j = json.loads(data)
    ids = []
    for record in j['data']:
        ids.append(record['uid2'])
    return ids
Ahmed Kotb
  • 6,269
  • 6
  • 33
  • 52
0

I had same problem. Though it is very late for answering question, it will help somebody. That's why answering this question.

Try this fql query:

$query="SELECT uid, name, work, education FROM user WHERE uid!=$source_id AND uid IN
                    (SELECT uid1, uid2 FROM friend WHERE uid1 IN (SELECT uid2 FROM friend WHERE uid1=$target_id AND uid2 = '$source_id' )
                        AND uid2 IN (SELECT uid2 FROM friend WHERE uid1=$target_id)) ORDER BY name";

$user_info=$this->facebook->api(array('method'=>'fql.query',
                'query'=>$query));

By this we can get mutual friends info.

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226