0

我有三张桌子:

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

我将如何查询这些表以获取客户名称和客户为其所有服务支付的总价格?

4

2 回答 2

1

你应该试试 :

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
于 2013-02-05T22:47:45.430 回答
0
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
于 2013-02-05T22:53:24.370 回答