This is part of my query:
WHERE WEEK( data.time ) = ( WEEK( CURRENT_DATE + INTERVAL -3 WEEK ) )
I can select data from three weeks ago, that means December 2012.
Now the problem is – I get data for that week for all years. Not just the one that has been three weeks ago.
Adding
YEAR( data.time ) = YEAR( CURRENT_DATE + INTERVAL -3 WEEK )
Solved the problem, but I wonder if it is necessary to compare the years also, or if I could stay with the week comparison only – if I adjust the first line somehow.
I’ve found similar query in here
MySQL week numbers and New Year
but they don’t mention such an issue with years and it seems it works for them even without year comparison.
You will be better served by comparing with the start and end of the required week. Something like:
It will make a difference for the first and last week of the year. For example 2012-12-31 is part of 2013-W01 (first week of 2013). So
WEEKwould say1whileYEARwould say2012. Whereas for 2013-01-13, also part of 2013-W01WEEKis1andYEARis2013. So naïve use ofWEEKandYEARfor this will give you the wrong results.