2016-06-24 59 views
0

我想显示销售项目的报告。列与分组的总和并将其显示在列表中

产品表

id,name,information,stock,MRP 

销售表

id,quantity,sales_price,product_id,sold_quantity. 

现在我想告诉所有的细节havng所有数量的总和的sold_quantity。

下面的查询运行,但individually.I想在1次像

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; 

回答

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 
相关问题