Creating SQL Reports Based on Date Criteria - Current Month (Page 3 of 4 )
If you want to see the data for the current month you can test both month and year in theWHEREclause:
mysql>SELECT * FROM t -> WHERE MONTH(whn)=MONTH(CURRENT_DATE) -> AND YEAR(whn)=YEAR(CURRENT_DATE) -> ORDER BY whn; +------------+------+ | whn | v | +------------+------+ | 2006-06-07 | 96 | | 2006-06-11 | 4 | | 2006-06-12 | 78 | | 2006-06-12 | 36 | | 2006-06-17 | 57 | | 2006-06-29 | 74 | | 2006-06-29 | 94 | | 2006-06-30 | 26 | +------------+------+
If you want to see the data for the preceding month do not changeMONTH(whn)=MONTH(CURRENT_DATE)toMONTH(whn)=MONTH(CURRENT_DATE)-1. If you do that you will get data from the wrong year when you run this query in January. Instead, you need to subtract one month fromCURRENT_DATE. It is a little neater if you do the date calculation in a nestedSELECT: