Skip to content

Instantly share code, notes, and snippets.

@evizitei
Created June 4, 2012 20:04
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 evizitei/2870544 to your computer and use it in GitHub Desktop.
Save evizitei/2870544 to your computer and use it in GitHub Desktop.
a useful sql statement to avoid a subquery
self.class.find_by_sql(["SELECT users.* FROM users
INNER JOIN connections AS second_degree_connections
ON second_degree_connections.target_id = users.id
INNER JOIN connections AS first_degree_connections
ON first_degree_connections.target_id = second_degree_connections.owner_id
WHERE first_degree_connections.owner_id = ?", self.id])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment