user3030814 user3030814 - 3 months ago 35
MySQL Question

Mysql query to get total current sales of each product by month

It is my sql code i want to show the Total SUM of current-sales by month of each product, in this image we can see the Total result in october of pro_id =175, i want to show the entry of every month of each product, is there any loop for many products? I will show the result in html table using php.

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

Answer Source

Use GROUP BY. With this, you'll get the sum of the current sell of each pro_id by the months of 2017

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download