MySQL: receive data only per months
Few times ago, I asked how to do to display data per month, I must told a bad explanation because I just figured out that it's not what I want :
Here's what I got :
$req1 = ...
AND v.date > (DATE_SUB(CURDATE(), INTERVAL 2 MONTH))
AND v.date < (DATE_SUB(CURDATE(), INTERVAL 1 MONTH))
$req2= ...
AND v.date > (DATE_SUB(CURDATE(), INTERVAL 3 MONTH))
AND v.date < (DATE_SUB(CURDATE(),开发者_StackOverflow社区 INTERVAL 2 MONTH))
But the problem, imagine that today you are the 10th June, it's going to calculate ALL the data between the
- 10 june to the 10 may
- then the 10 may until the 10 april...
But what I want is data :
- from 1st may to 1 st june,
- from 1st june to 1st july...
Do you see what I mean ?
You could use:
WHERE YEAR(date) = 2010 AND MONTH(date) = 5
to get all rows where date is in the YEAR 2010 and the fifth month of the year.
AND MONTH(v.date)=6 AND YEAR(v.date)=2009 [to get everything in June 2009]
select month(v.date), year(v.date), sum(somedatacolumn) from thetable group by month(v.date), year(v.date);
replace sum with whatever calculation you are doing
精彩评论