我正在使用3个表的连接计算mysql中每个客户的总花费

时间:2014-04-27 19:59:46

标签: mysql join

我有4张桌子:

customer(customer_id,last_name,first_name)

产品(isbn,书名,ListPrice)

order_has_products(customer_id,order_id,isbn)

订单(order_id,customer_id)

我想计算每位顾客在书​​上花费的总金额。在我看来,我将加入3张桌子。我需要一个"数量"列能够做到这一点吗?

1 个答案:

答案 0 :(得分:0)

SELECT SUM(products.ListPrice) FROM order_has_products
INNER JOIN products on order_has_products.isbn = products.isbn
GROUP BY order_has_products.customer_id