写查询以获得两列的乘数之和

时间:2016-07-07 10:40:34

标签: mysql

我已经编写了查询以获得两列乘法的总和。

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

在这里,如图所示,我想要所有记录以及总数的总和。

那么,我该怎么做?

1 个答案:

答案 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

此查询运行完美。