mister martin
mister martin

Reputation: 6252

Sum between multiple tables

I have three tables:

Services
ID    Name       Price
1     Internet   99.99
2     Phone      49.95
3     TV         159.95

Customers
ID    Name
1     Ryan
2     Simon
3     Jimmy

Customer Services
CustomerID ServiceID
1          1
1          2
2          3

How would I query these tables to get both the customer name and the total price the customer pays for all of their services combined?

Upvotes: 0

Views: 55

Answers (2)

sdespont
sdespont

Reputation: 14025

You should try :

SELECT c.name, SUM(s.price) FROM services s
INNER JOIN customer_services sc ON cs.service_id = s.id
INNER JOIN customers c ON c.id = cs.customer_id
GROUP BY c.id

Upvotes: 1

Tony
Tony

Reputation: 2754

select 
   c.name, 
   sum(s.price) 
from ((
   customers c
      inner join custserv cs on cs.cid = c.id )
      inner join services s on s.id = cs.sid )
group by 
   c.id

Upvotes: 0

Related Questions