我目前正在显示总订单价值和订单数量按订单被标记为已发送的日期分组,限制为5.此数据来自我的'订单'表。
SELECT COUNT(orders_id) AS order_qty,
DATE_FORMAT(last_modified, "%a %D %b") AS day,
SUM(order_total) AS daily_total
FROM orders
WHERE orders_status = 3
GROUP BY DATE(last_modified)
ORDER BY DATE(last_modified) DESC
LIMIT 5
结果示例:
我想要包含这些订单中包含的商品总数。这些数据存储在我的'orders_products'表中。在此表中,我有一个名为 products_quantity 的列,如果订单号包含在原始查询中,我需要SUM。
我知道我需要使用联接,但如果订单表中的 orders_status ,则不知道如何编写查询以对结果进行分组= 3 然后按发货日期分组?
有人能指出我正确的方向吗?
答案 0 :(得分:0)
将order_products表加入订单表并直接汇总。
这可能有效,我猜测order_products中的字段名称:
SELECT COUNT(orders_id) AS order_qty,
DATE_FORMAT(last_modified, "%a %D %b") AS day,
SUM(order_total) AS daily_total,
SUM(order_products.products_quantity) as total_items
FROM orders
LEFT OUTER JOIN order_products on order_products.orders_id=orders.orders_id
WHERE orders_status = 3
GROUP BY DATE(last_modified)
ORDER BY DATE(last_modified) DESC
LIMIT 5
联接记录将根据联接表的GROUP BY子句进行分组,并相应地汇总值。