列与组的总和,并在列出的表中显示

时间:2016-06-24 11:23:51

标签: mysql sum

我想显示销售项目的报告。

产品表

id,name,information,stock,MRP

销售表

id,quantity,sales_price,product_id,sold_quantity.

现在我想要显示所有数量总和的sold_quantity的所有细节。

低于查询运行但是单独运行。我想在一次显示两者,如

enter image description here

SELECT product_id,sum(quantity) as quantity FROM sales GROUP BY product_id;
select product.name,product.information,sales.cost_price,sales.quantity from sales inner join product on sales.product_id=product.id;

1 个答案:

答案 0 :(得分:0)

您可以使用子查询来执行此操作,尝试此操作;)

select product.name, product.information, sales.cost_price, sales.quantity, t.quantity as sold_quantity
from sales
inner join product on sales.product_id=product.id
left join (
    select product_id, sum(quantity) as quantity
    from sales
    group by product_id
) t on t.product_id = product.id