我在opencart网站上使用此查询
SELECT MIN(tmp.date_added) AS date_start,
MAX(tmp.date_added) AS date_end,
COUNT(tmp.order_id) AS `orders`,
SUM(tmp.products) AS products,
SUM(tmp.tax) AS tax,
SUM(tmp.total) AS total
FROM
( SELECT o.order_id,
( SELECT SUM(op.quantity)
FROM `oc_order_product` op
WHERE op.order_id = o.order_id
GROUP BY op.order_id
) AS products,
( SELECT SUM(ot.value)
FROM `oc_order_total` ot
WHERE ot.order_id = o.order_id
AND ot.code = 'tax'
GROUP BY ot.order_id
) AS tax,
o.total,
o.date_added
FROM `oc_order` o
WHERE o.order_status_id > '0'
AND DATE(o.date_added) >= '2015-03-01'
AND DATE(o.date_added) <= '2016-04-19'
GROUP BY o.order_id
) tmp
GROUP BY WEEK(tmp.date_added)
ORDER BY tmp.date_added DESC
LIMIT 0,60
答案 0 :(得分:0)
WEEK
将在今年的第一天出现打嗝 - 将会有两个部分时间。
我们现在处于&#34;周&#34; 2016年16月。这相当于2015年略有不同的日子;你想要他们合并吗?
由于WEEK
的这些打嗝,您最好将最终ORDER BY
更改为WEEK(tmp.date_added) DESC
FROM ( SELECT ... )
可能很好。那是你要问的吗?
这两个( SELECT SUM ... ) AS ...
可能是最佳的,或几乎是最佳的。那是你要问的吗?
但是,您可能需要一些索引:
oc_order_total: INDEX(code, order_id) -- in that order
oc_order_product: INDEX(order_id)
将DATE(o.date_added) >= '2015-03-01'
更改为o.date_added >= '2015-03-01'
(等),以便INDEX(date_added)
可以使用。
如果这只是&#39; 1&#39;:o.order_status_id > '0'
,请将其更改为o.order_status_id = 1
,以便INDEX(order_status_id, date_added)
可以使用。