Skip to content

Instantly share code, notes, and snippets.

@lqc
Created August 9, 2010 20:46
Show Gist options
  • Select an option

  • Save lqc/516088 to your computer and use it in GitHub Desktop.

Select an option

Save lqc/516088 to your computer and use it in GitHub Desktop.
SELECT DISTINCT people.*, count(my_friends.guid_two) AS coincidence
FROM entities people
LEFT JOIN metadata
ON (metadata.entity_guid = people.guid)
LEFT JOIN relationships related_friends
ON (related_friends.guid_two = people.guid)
LEFT JOIN relationships my_friends
ON (related_friends.guid_one = my_friends.guid_two)
WHERE my_friends.guid_one = 'ME!' -- indirectly related to me
AND my_friends.relation = 'friend'
AND related_friends.guid_one != 'ME!' -- not related directly to me
AND related_friends.relation = 'friend'
AND (metadata.name = 'hobby' AND metadata.value IN ('sport', 'ksiazki')) -- IN my_hobbies
GROUP BY my_friends.guid_two
HAVING coincidence >= 1
ORDER BY coincidence DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment