I wrote this query and it is working fine, except in the month of FEB! for some reason because FEB has 28 days by using the INTERVAL 1 MONTH function it reads back into JAN also. and it should only read from start of FEB to END of FEB!
SELECT SUM(IF((`date` BETWEEN DATE_SUB('2013-02-28', INTERVAL 1 MONTH) AND '2013-02-28'), earnings, "null")) AS Rev0
FROM table
Given any date, you can use this to get the first day of the month:
or also this:
and for the last day, there’s a function already defined:
your query would then become: