gpt4 book ai didi

mysql - 多个表之间的求和

转载 作者:行者123 更新时间:2023-11-29 14:03:33 25 4
gpt4 key购买 nike

我有三个表:

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

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

最佳答案

你应该尝试:

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

关于mysql - 多个表之间的求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14718360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com