如何从mysql中的内连接中获取一个表的总和

时间:2017-12-28 07:38:11

标签: php mysql

我有两个表,一个是订单,第二个是order_product,其中我必须找出订单数,产品数,totalamount对应的 客户使用买家ID,我已成功找到订单数量和产品数量,但我的totalamount不正确。

订单:

.......................................
 order_id   or_buyer_id   or_total_amt
.......................................
    1           21           10
    2           22           10
    3           21           10

order_product

.................................
op_id  op_order_id  op_buyer_id
.................................
  1       1          21
  2       1          21
  3       2          22
  4       3          21

我想要低于输出,但我的totalamount值出现了错误 30,但正确的值是20,我在下面右边的输出中提到过。

我想要的输出:

............................................
or_buyer_id  orders  product   totalmount
...........................................
   21         2       3          20
   22         1       1          10

我已经尝试过以下查询,它给出了30个值为totalamount的错误。

SELECT o.or_buyer_id
     , count(DISTINCT o.order_id) as orders
     , count(op.op_id) as product
     , SUM(o.or_total_amt) as totalamount 
  FROM orders as o
  JOIN order_product as op 
    on op.op_order_id = o.order_id 
   and o.or_buyer_id = op.op_buyer_id 
 group 
    by o.or_buyer_id

3 个答案:

答案 0 :(得分:1)

一种方法是从子查询中的order_product表中删除重复的行。然后,将其加入orders,就像您已经在做的那样。这里的一个小技巧是,当删除子查询中的重复项时,我们在order_product表中保留原始记录的数量。这是因为product列反映了真实的原始记录数。但是totalamount并不反映重复,因此我们将此数量与删除的重复数量相加。

SELECT
    t1.op_buyer_id,
    COUNT(DISTINCT t1.op_order_id) AS orders,
    SUM(t1.cnt) AS product,
    SUM(t2.or_total_amt) AS totalamount
FROM
(
    SELECT op_order_id, op_buyer_id, COUNT(*) AS cnt
    FROM order_product
    GROUP BY op_order_id, op_buyer_id
) t1
INNER JOIN orders t2
    ON t1.op_order_id = t2.order_id AND
       t1.op_buyer_id = t2.or_buyer_id
GROUP BY
    t1.op_buyer_id
ORDER BY
    t1.op_buyer_id;

screen capture of demo query

Demo

答案 1 :(得分:0)

您应该在内部联接中使用子查询,而不是直接引用order_product表:

尝试将order_product替换为

(SELECT DISTINCT op_order_id, op_buyer_id
 FROM order_product) AS op

这将删除给你错误总和的重复项。

我还引入了一个子查询来获取正确的产品值:

SELECT count(op_order_id) FROM order_product WHERE order_id = o.order_id AND op_buyer_id = o.or_buyer_id) as product

最终的SQL是:

SELECT o.`or_buyer_id`, 
       count(DISTINCT o.`order_id`) as orders, 
       (SELECT count(op_order_id) FROM order_product WHERE order_id = o.order_id AND op_buyer_id = o.or_buyer_id) as product,
       SUM(o.`or_total_amt`) as totalamount 
FROM `orders` as o 
INNER JOIN (SELECT DISTINCT op_order_id, 
                            op_buyer_id
            FROM order_product) AS op 
ON op.op_order_id=o.order_id AND o.or_buyer_id = op.op_buyer_id 
GROUP BY o.`or_buyer_id`

答案 2 :(得分:0)

您应该使用此子查询

SELECT tbl.op_buyer_id, COUNT(DISTINCT tbl.op_order_id) AS orders, SUM(tbl.cont) AS product, SUM(tbl2.or_total_amt) AS totalamount FROM ( SELECT op_order_id, op_buyer_id, COUNT(*) AS cont FROM order_product GROUP BY op_order_id, op_buyer_id ) tbl INNER JOIN orders tbl2 ON tbl.op_order_id = tbl2.order_id AND tbl.op_buyer_id = tbl2.or_buyer_id GROUP BY tbl.op_buyer_id ORDER BY tbl.op_buyer_id