$listSQL = "SELECT op.name as prodname,count(*) total
FROM oc_order_product op
INNER JOIN oc_order o ON op.order_id = o.order_id
INNER JOIN oc_product_to_category p2c ON op.product_id = p2c.product_id
INNER JOIN oc_category_description cd ON cd.category_id = p2c.category_id ";
$listSQL = $listSQL."where lower(cd.name) LIKE '%".$category_name."%'
AND YEAR(o.date_added) = '".$StartDate."'
AND o.order_status_id > '0' ";
$listSQL = $listSQL."GROUP BY op.name ORDER BY o.date_added ASC";
我有这个查询,我显示产品名称和按年计数。
我想显示产品名称,并为每个产品显示该特定年份的月份和月份。
例如2015年,显示所有12个月,并显示该月的产品数量。
答案 0 :(得分:0)
您的原始查询看起来非常接近,但使用“分组外”的非分组,非聚合字段通常不是一个好主意,除非它们来自一个表,其完整主键是按列表分组的一部分。也就是说,这应该给你你想要的东西。
SELECT op.name AS prodname
, YEAR(o.date_added) AS oYear
, MONTH(o.date_added) AS oMonth
, COUNT(DISTINCT o.order_id) AS numOfOrders
FROM ...
GROUP BY prodname, oYear, oMonth
ORDER BY prodname, oYear, oMonth
;
[...]是您的原始FROM
和WHERE