Mysql查询以按月获得每个产品的当前总销售额

时间:2017-11-24 14:33:24

标签: php mysql sql

这是我的sql代码我想显示每个产品的当前销售额的总和,在这个图像中我们可以看到10月份的总结果pro_id = 175,我想显示每个月的条目对于每种产品,许多产品都有循环吗?我将使用php在html表中显示结果。

SELECT  MONTHNAME(`order_date`), SUM(`current-sales`) AS TotalCS,  `nsp`, `pro_id`
FROM `orders` 
WHERE YEAR(order_date) = 2017 AND MONTH(order_date) = 10
  AND pro_id = 175

Result by October

2 个答案:

答案 0 :(得分:0)

使用GROUP BY。有了这个,您将获得2017年之前每个pro_id当前卖出的总和

SELECT  MONTHNAME(order_date), SUM(current-sales) AS TotalCS, pro_id
FROM orders
WHERE YEAR(order_date) = 2017
GROUP BY MONTH(order_date), pro_id, nsp

答案 1 :(得分:0)

尝试使用datepart函数:

SELECT datepart('month','order_date'), SUM(`current-sales`) AS TotalCS, `nsp`, `pro_id` 
FROM `orders` 
WHERE datepart('year','order_date') = '2017' AND datepart('month','order_date')= '10' AND pro_id = 175

十月结果

SELECT datepart('month','order_date'), SUM(`current-sales`) AS TotalCS, `nsp`, `pro_id` 
FROM `orders` 
WHERE datepart('year','order_date') = '2017' AND datepart('month','order_date')= '10' AND
Group by datepart('month','order_date');

整个月