Reputation: 389
I'm trying to INNER join 3 tables. 1st JOIN (alone) works perfect and returns the expected 3 rows. I then add another join to say match a name column from a third table. I would expect just 3 rows again but NO. I get back a TON of rows.
My question is, do JOINs build off of each other? 1st INNER JOIN gives us results_1 ... 2nd INNER JOIN on results_1 gives results_2 ... 3rd INNER JOIN on results_2 and so on?? Is this true?
select
students.*
FROM classes
INNER JOIN students ON students.id = classes.id
INNER JOIN books ON books.obj_num_position = students.object_table_obj_num_pos_ref
I find myself falling back to using FROM X Y Z ... and WHERE x.id = z.id AND WHERE AND WHERE ..... instead of explicit joins as its easier to troubleshoot
Upvotes: 2
Views: 144
Reputation: 270637
The INNER JOIN
will return as many rows as there are matches across the three tables. In your case, it would seem that students.object_table_obj_num_pos_ref
has a one-to-many relationship with books.obj_num_position
, and therefore returns every books.obj_num_position
matching any students.object_table_obj_num_pos_ref
having one of the students.id
returned in the previous JOIN
with classes
.
If you need to limit to a subset of these, the way to do so is still via a WHERE
clause, in which you specify the limiting condition.
By the way, if you used an implicit join with no conditions other than the joining conditions in the WHERE
clause, you would get exactly the same result.
Upvotes: 1