I'm trying to fetch out all month names with the value of zero if data does not exist.
Here is what I achieved so far
invoice_order table
order_id | user_id | order_date | order_total_amount
1 | 1 | 01-01-2021 | 10000
2 | 1 | 02-02-2021 | 20000
The SQL query would pick the order_total_amount of the first row (10000) which would store in January month. but for February, it is showing order_total_amount zero
value zero needs to show up only when no data exists
$monthly_sale = "SELECT
SUM(IF(month = 'Jan', total, 0)) AS 'Jan',
SUM(IF(month = 'Feb', total, 0)) AS 'Feb',
SUM(IF(month = 'Mar', total, 0)) AS 'Mar',
SUM(IF(month = 'Apr', total, 0)) AS 'Apr',
SUM(IF(month = 'May', total, 0)) AS 'May',
SUM(IF(month = 'Jun', total, 0)) AS 'Jun',
SUM(IF(month = 'Jul', total, 0)) AS 'Jul',
SUM(IF(month = 'Aug', total, 0)) AS 'Aug',
SUM(IF(month = 'Sep', total, 0)) AS 'Sep',
SUM(IF(month = 'Oct', total, 0)) AS 'Oct',
SUM(IF(month = 'Nov', total, 0)) AS 'Nov',
SUM(IF(month = 'Dec', total, 0)) AS 'Dec'
FROM (
SELECT DATE_FORMAT(order_date,'%M') AS month, SUM(order_total_amount) as total
FROM invoice_order
WHERE user_id='$user_id' group by year(order_date),month(order_date) order by year(order_date),month(order_date)) as sale";
the above query would return all month names along with zero value (if data doesn't exist). but it showing order_total_amount of only the first row. To make it more simplified, it picking up the order amount of only one month