2016-06-24 61 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 
相關問題