If your fecha
field does not include time, then you can use BETWEEN
without problems.
But just in case, I'll leave you with another option that works fine regardless of whether the fecha
field includes a one hour portion or not. And it also has the advantage that you do not need to remember how many days you have the month, or to have to adjust the number of days depending on the year if the month you are interested in is February.
select count(*)
from usuario
where fecha >= '2016-07-01'
and fecha < '2016-08-01'
Edit
With the new clarifications, you can use the following query:
select MonthName(fecha), count(*)
from usuario
where year(fecha) = year(curdate())
group by MonthName(fecha)
... or, although it looks a bit more complicated, the following query is more optimal if you have an index defined in the fecha
column and you have many records with different years. The optimization is due to the fact that I do not apply the function year
to the column fecha
, which allows the use of the index, if you have it:
select MonthName(fecha), count(*)
from usuario
where fecha >= makedate(year(curdate()), 1)
and fecha < makedate(year(curdate()) + 1, 1)
group by MonthName(fecha)