Skip to content
Advertisement

How to get a count even if there are no results corresponding mysql?

I am formalating a query to give the number of reports submitted over the last year ordered by date. I get the current year and month with php:

and execute the following query: SQL:

And because there has only been 1 submitted in the last year it gives me only 1 result…

But I would like the result set to show:

Is that possible?

Advertisement

Answer

You should LEFT JOIN this table with 1..12 table. Something like this:

SQL Fiddle demo

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement