我有这3张桌子:
比尔:
idBill
产品:
idProduct
price
BillProducts (连接上表):
idBill
idProduct
quantity
现在让我们说我希望得到一个由其身份证明的特定账单的总价
我需要将Products.Price
的列乘以BillProducts.quantity
,得到其结果并汇总idBill
你们可以帮我写一下这个查询吗?
答案 0 :(得分:3)
SELECT SUM(QUANTxPRICE) AS SUMED, IDBILL FROM (
SELECT (A.QUANTITY * B.PRICE) AS QUANTxPRICE,a.IDBILL
FROM BILLPRODUCTS AS A
JOIN PRODUCTS AS B ON
A.IDPRODUCT = B.IDPRODUCT
JOIN BILL AS C
ON A.IDBILL = C.IDBILL
) AS X
GROUP BY IDBILL
答案 1 :(得分:1)
你会这样做:
select sum(p.price * bp.product)
from billproducts bp join
products p
on bp.idproduct = p.idproduct
where idbill = <idbill>;
您可以使用group by
select idbill, sum(p.price * bp.product)
from billproducts bp join
products p
on bp.idproduct = p.idproduct
group by idbill;
注意:
join
bills
表。您需要的所有信息都在另外两个表格中。