2016-07-07 9 views
0

2つの列の乗算の合計を得るためのクエリを書きました。2つの列の乗算の合計を得るためのクエリを書く

select *,sum(op.qty*op.price) as total from `order` as o 
join order_products as op on op.order_id = o.order_id 
where o.user_id = 5 and o.order_id = 10 group by op.order_product_id 

私はすべてのレコードだけでなく、全体の合計をしたい画像に示されているように私の出力は、ここで enter image description here

のように見えます。

どうすればいいですか?

+0

サンプルデータと期待される出力を表示できますか?質問の理解に役立ちます.. –

答えて

0

私は

select 
t1.*, 
t2.total 
FROM 
(
    SELECT op.* 
    FROM `order` AS o JOIN order_products AS op ON op.order_id = o.order_id 
    WHERE o.user_id = 5 AND o.order_id = 10 
) as t1 
LEFT JOIN 
(
    SELECT sum(op.qty * op.price) as total 
    FROM `order` AS o JOIN order_products AS op ON op.order_id = o.order_id 
    WHERE o.user_id = 5 AND o.order_id = 10 
) as t2 
ON 1 = 1 

としての私のクエリを変更して、それがこの

enter image description here

と完璧な実行し、このクエリのような結果となります。

関連する問題