0

I have a list of dates from table like this:

2014-05-21
2014-08-20
2014-06-03
2014-06-03
2014-05-24
2014-05-28
2014-05-24
2014-05-27
2014-08-20
2014-05-28
and so on.

I need to count the total grouped by month for each date like this: 05 = 6, 06 = 2, 08 = 2.
I am very noob in mysql, can you guys help me on how I should do this? Thanks.

3
  • 1
    Does it matter that they might not all be the same year? Commented Oct 26, 2014 at 8:33
  • Yes I just noticed that :| I may just have to include WHERE YEAR(date) = 2014 in the query. Wait, that is wrong I guess. Commented Oct 26, 2014 at 8:35
  • 1
    You could, but that cannot use an index. Better to say " date between '2014-01-01' and '2014-12-31' " Commented Oct 26, 2014 at 8:41

3 Answers 3

2

You can do so

select month(`date`) `month`,
count(*)
from test 
group by `month`

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Any idea on how do I count if it is not the same year?
@ShahErhan you can change group by to group by year(date),month and for select month(date) month, year(date)
1

In order to count by month and year you should add a year column in your result and add it to group by.

   select YEAR(`date`)  `year`,MONTH(`date`) `month`,
   count(*)
   from test 
   GROUP BY YEAR(`date`), MONTH(`date`) DESC;

Comments

0

try this query it would work, may be with some alteration

 "select count(DATE_FORMAT(date_column, '%m')) form your_table group by date_coloumn"

or

 "select count(*),DATE_FORMAT(date_column, '%m') form your_table group by date_coloumn"

2 Comments

I've tried your solution with some alteration but I can't get it right. The other answer works flawlessly. Thanks by the way.
i have a mistake in form instead of form, any ways purpose is achieved and thats great (Y)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.