-1

这是我的桌子

表 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

谢谢

4

6 回答 6

1

您应该使用子查询来计算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'
于 2016-04-22T10:26:35.130 回答
1

也许你错过了GROUP BY条款

(SELECT SUM(dop.qty) FROM  order_products dop WHERE dop.order=ddo.id GROUP BY dop.order)
于 2016-04-22T10:21:05.577 回答
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

于 2016-04-25T07:04:59.390 回答
0

获得答案的最简单的查询是,这就像一把刀穿过黄油。

但是,如果您为列 order(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;
于 2016-04-26T18:51:14.137 回答
0

看起来你想总结一下order_productsorders然后总结一下结果。

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

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

如果您想要单独orders运行prd_qty内部查询。

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

作为旁注,我会仔细研究您的命名约定。每个人都有自己的喜好,但您应该与以下问题保持一致:

  • 缩写?- tot, total, prd, product,qty
  • 附加_id外键?- order_products.order,order_products.product_id
  • 在列中包含表名?- orders.total,orders.order_date

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

就我个人而言,我通常不缩写,添加_id并且不在列中包含表名。

于 2016-04-25T13:33:18.660 回答
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;

相当简单。

于 2016-04-27T05:59:17.733 回答