Mysql – Sequence of dates – months from January to December with SQL

mariadbMySQL

How can I create in SQL a function to search how many courses does a company each month from January to December in a specific year (2015)?

In the table fechahora form cursos, I have 1 in June, 1 in July, 3 in August, 1 in November and 0 in the other months. Furthermore I have the dates in this format '2015-01-01' and I need the name of the month.

I'm trying with this SQL statement.

SELECT 
  MONTHNAME(FECHAHORA) AS 'MONTH', 
  COUNT(Monthname(FECHAHORA)) AS 'TOTAL' 
FROM COURSES 
WHERE DATEOFCOURSE BETWEEN '2015-01-01' AND '2015-12-31' 
GROUP BY MES 
ORDER BY FECHAHORA;

From this statement, I have the results from the months that have a course but I need from January to December.

table and result

Best Answer

If you are using MariaDB, use something like this:

SELECT '2015-01-01' + INTERVAL seq MONTH FROM seq_0_to_11;

More discussion: https://mariadb.com/kb/en/library/sequence-storage-engine/