简体   繁体   中英

How to get total sum from one table with inner join in mysql

I have two tables one is orders and second is order_product in which I have to find out orders count, product count, totalamount in corresponding to customer using buyers id from which I have successfully find out the orders count and product count but my totalamount is not coming correct.

orders:

.......................................
 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

I want below output but my totalamount value is coming wrong it is coming 30,but the correct value is 20 which i have mentioned in the right output below.

output which i want:

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

I have tried the below query which is giving 30 value of totalamount which is wrong.

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

One approach is to remove the duplicate rows from the order_product table in a subquery. Then, join that to orders as you were already doing. One slight trick here is that we maintain the number of original records in the order_product table when removing duplicates in the subquery. This is because the product column reflects the true original record count. But the totalamount does not reflect duplicates, hence we sum this quantity with duplicates removed.

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;

演示查询的屏幕截图

Demo

You should use a subquery in the inner join instead of referring directly to order_product table:

Try to substitute order_product with

(SELECT DISTINCT op_order_id, op_buyer_id
 FROM order_product) AS op

This will remove duplicates that are giving you the wrong sum.

I also introduced a subquery to get the correct product value:

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

The final SQL is:

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`

你应该使用这个子查询

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM