Request in MySQL in two tables [closed]

Just join, group by and sum(). Assuming that the foreign key column in table2 is t1_id:

select t1.name, sum(t2.cost) total_cost
from table1 t1
inner join table2 t2 on t2.t1_id = t1.id
group by t1.id, t1.name

If you want to allow names that have no match in table2, then use left join instead, and maybe coalesce(sum(t2.cost), 0) as well.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top