Reputation: 453
So, i'm fighting with that, can't find the solution. Here's problem: We have three tables. The first one is connected to second by id, the third is connected to second as well by id, and there's no connection between first an third.
I need to join name
from first and sum of id
from third table.
i tried to use subqueries, but failed to organize joining.
Any suggestions?
Upvotes: 0
Views: 159
Reputation: 383
In a three table join query, you could something like this:
SELECT tab1.name, COUNT(tab3.id) AS sum_ids
FROM tab1 INNER JOIN tab2 ON tab1.id=tab2.tab1_id
INNER JOIN tab3 ON tab2.id=tab3.tab2_id GROUP BY tab1.id;
Upvotes: 1
Reputation: 76424
So you have a three-level category problem. This might help you
select Tab1.name, sum(Tab3.id)
from Tab1
join Tab2
on Tab1.id = Tab2.tab1_id
join Tab3
on Tab2.id = Tab3.tab2_id
group by Tab1.id
Upvotes: 1