left outer joins in rails/arel and left outer joins with where
Consider the following two queries:
1 2 3 4 5 |
SELECT cats.id, cats.name FROM movies LEFT OUTER JOIN people ON(cats.id=people.cat_id AND people.name='John') LIMIT 10; |
1 2 3 4 5 6 7 |
SELECT cats.id, cats.name FROM movies LEFT OUTER JOIN people ON(cats.id=people.cat_id) Where people.name='John' LIMIT 10; |
The first query returns filters the joined table, but NOT the final results. The second query filters the final results. So, the first query will return 10 rows of cats, whether or not there are any people named john, and the second row will only return cats if there are people named john.
Also—in rails a left outer join is relation.incude, while a normal join is relation.joins.
April 20, 2011
