开发者

Calculate sum from three tables

开发者 https://www.devze.com 2023-03-13 04:28 出处:网络
I have 3 mysql tables: Client_courses Client_courses_id Client_id Course_id 112 213 321 Client Client_id Name

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
0

精彩评论

暂无评论...
验证码 换一张
取 消