Calculate sum from three tables
I have 3 mysql tables:
Client_courses
Client_courses_id Client_id Course_id
1 1 2
2 1 3
3 2 1
Client
Client_id Name
1 Tom
2 John
Courses
Course_id Name Price
1 Microsofr 100
2 Programming 250
3 Leadership 300
I need 开发者_Go百科to calculate how much every client spent money on courses. For example: Tom spent 550 (250+300), John spent 100. And I am confused how to do it.
SELECT SUM(c.Price), cl.Name
FROM Client cl
INNER JOIN Client_courses clc ON cl.Client_id=clc.Client_id
INNER JOIN Courses cs ON clc.Course_id=cs.Course_id
GROUP BY cl.Name
精彩评论