Skip to content

Instantly share code, notes, and snippets.

@maletor
Last active September 22, 2016 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maletor/dfbaf97fe451b9f6d4b41057f5259b3e to your computer and use it in GitHub Desktop.
Save maletor/dfbaf97fe451b9f6d4b41057f5259b3e to your computer and use it in GitHub Desktop.
Query mutual friends through joining the friendships table twice
SELECT `users`.* FROM `users`
INNER JOIN `friendships` `a` ON `users`.`id` = `a`.`friend_id`
INNER JOIN `friendships` `b` ON `users`.`id` = `b`.`friend_id`
WHERE `a`.`user_id` = 1 AND `b`.`user_id` = 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment