Mysql从两个表中获取SUM和COUNT值

时间:2016-04-22 10:17:02

标签: mysql nested-select

这是我的表

表1:订单

id  total order_date
1   200    2016-04-22
2   300    2016-04-22

表2:order_products

 id  order product_id qty
  1    1       1       2
  2    1       2       1
  3    2       2       2
  4    2       1       2

我的结果应该是

tot_order total_amount  prd_qty
 2      500             7 

我的查询是

    SELECT COUNT(ddo.id) AS tot_order, 
           SUM(ddo.total) AS total_amount, 
           (SELECT SUM(dop.qty) 
            FROM  order_products dop 
            WHERE dop.order=ddo.id) AS prd_qty  
    FROM orders ddo 
    WHERE DATE(ddo.`order_date`) BETWEEN '2016-04-22' AND '2016-04-22'

我可以获得total_order和total_amount,但是如何获得prd_qty

由于

6 个答案:

答案 0 :(得分:1)

也许您错过了GROUP BY子句

(SELECT SUM(dop.qty) FROM  order_products dop WHERE dop.order=ddo.id GROUP BY dop.order)

答案 1 :(得分:1)

此查询适用于您。

SELECT COUNT(od.`id`) AS tot_order, SUM(od.`total`) AS total_amount, tablea.prd_qty
FROM orders od JOIN (
                SELECT SUM(op.`prd_qty`) AS prd_qty
                FROM orders od INNER JOIN order_products op
                ON od.`id`=op.`order`
                WHERE DATE(od.`order_date`) BETWEEN '2016-04-22' AND '2016-04-22' 
) tablea
WHERE DATE(od.`order_date`) BETWEEN '2016-04-22' AND '2016-04-22'

要获取prd_qty,您必须在join之后运行我在小括号中添加的子查询。

答案 2 :(得分:0)

您应该使用子查询来计算prd_qty:

SELECT SUM(dop.qty) as prd_qty,  dop.order 
FROM  order_products dop 
GROUP BY dop.order

每个订单都计算prd_qty 完整的查询:

SELECT COUNT(ddo.id) AS tot_order, 
       SUM(ddo.total) AS total_amount, 
       sum(op.prd_qty) as prd_qty
FROM   orders ddo 
       JOIN 
       (
            SELECT SUM(dop.qty) as prd_qty,  dop.order 
            FROM  order_products dop 
            GROUP BY dop.order
       ) as op ON (op.order = ddo.id)
WHERE  DATE(ddo.`order_date`) BETWEEN '2016-04-22' AND '2016-04-22'

答案 3 :(得分:0)

您希望将order_products汇总到orders,然后总结结果。

我会在查询中遵循这个概念:

SELECT COUNT(*) tot_order,
       SUM(os.total) total_amount,
       SUM(os.o_prd_qty) prd_qty
  FROM (       
    SELECT o.id,
           o.total,
           SUM(op.qty) o_prd_qty
      FROM orders o
      JOIN order_products op
        ON op.order = o.id
     WHERE o.order_date >= :start_date
       AND o.order_date < :end_date + INTERVAL 1 DAY
  GROUP BY o.id
       ) os

如果您希望单独的ordersprd_qty一起运行内部查询。

我还修复了日期逻辑,以便优化器可以使用order_date上的索引。通常,如果在比较的一侧将函数中的列包装,则该列上的任何索引都将无法使用。

作为旁注,我会仔细研究你的命名惯例。每个人都有自己的偏好,但你应该与这些问题保持一致:

  • 缩写? - tottotalprdproductqty
  • 为外键添加_id - order_products.orderorder_products.product_id
  • 在列中包含表名? - orders.totalorders.order_date

遵循约定使您的数据库更易于导航和使用。否则,在编写每个查询时,它都会返回模式!

我个人通常不会缩写,请附加_id,不要在列中包含表名。

答案 4 :(得分:0)

获得答案的最简单的查询是,这将作为一刀通过黄油。

但是,如果为列订单(id)创建索引,它会更漂亮。如果它已经是主键列,则不需要索引 仅当您认为数据的数量会增加

时,才会建立索引
select count(o.id) as tot_order ,
sum(o.total) as total_amount,
(select sum(p.qty) from orders o join order_product p on o.id=p.order) as prd_qty     
from orders o;

答案 5 :(得分:0)

  

这应该适合你。

select sum(order_id) orders, sum(total) total, sum(qty) prd_qty
from (select count(o.id) order_id, sum(o.total) total,
(select sum(qty) from order_products op where o.id = op.`order`) qty
from orders o group by o.id) x;

相当简单。