Se ho capito bene, devi usare un OUTER JOIN
per ottenere risultati per i mesi mancanti, ma è necessario anche un CROSS JOIN
per ottenere il nome dell'azienda correttamente -- in caso contrario, verrà visualizzato come NULL
:
SELECT months.month,
SUM(job_details.price_each*job_details.quantity) AS sum_monthly_price,
DATE_FORMAT(job.order_date, '%M') AS order_date,
customer.company_name
FROM months CROSS JOIN customer
LEFT JOIN job on job.company_id = customer.company_id
AND months.month = month(job.order_date)
LEFT job_details on job.job_id = job_details.job_id
WHERE months.month > month(date_sub(NOW(), INTERVAL 4 month))
AND months.month <= month(NOW())
AND customer.company_id = 6
GROUP BY months.month
ORDER BY months.month asc