简体   繁体   English

多个表之间的总和

[英]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? 我如何查询这些表以同时获得客户名称和客户为其所有服务所支付的总价?

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
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM