Checking Whether Two Users Are in the Same Group

Problem

I need to check to see whether two specific users are in the same group using FQL.

Solution

Assuming the current loggedinuser is able to see both groups:

SELECT name FROM group WHERE gid IN (SELECT gid FROM
 group_member WHERE uid = $user1 AND gid IN (SELECT gid FROM group_member
 WHERE uid = $user2))

You’ll need to plug the two IDs into user1 and user2.

Discussion

This should be pretty easy to extend for more than two users if you need to, and it shouldn’t affect performance considerably, since the heavy lifting is done by the database. Just chain on an additional subquery for every additional user you need to check, and remember to add a closing bracket at the end.

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.