I have a table with about 5 years of dates (2011 - 2016).
Each date is stored in a column called "dt" as YYY-MM-DD (ie. 2012-01-04) and "y" as year (2012, 2013). I also have a column called "w" which is the week number for each week in the calendar year which helps. It resets to 0 for the first week of each year and then increments to 52 to the last week.
I am trying to find the correct MYSQL SELECT query to extract 4 week blocks (pay periods) from each calendar year.
So for 2014, one would expect 13 pay blocks.. I want to return the results as follows:
Period 1
Week 1 (2012-12-31 to 2012-01-05)
Week 2 (2012-01-06 to 2012-01-12)
Week 3 (2012-01...
Week 4 (2012..
Period 2
Week 5 (2012..
Week 6 (2012..
Week 7 (2012..
...
Period 3
..
..
Period 13
..
Code:
$currentYear="2014"
for ($i = 0; $i <= 52; $i++) {
$sql="SELECT * FROM calendar WHERE Y='$currentYear' AND w=$i GROUP BY w";
$result=mysql_query($sql);
while($row=mysql_query($result)){
... is this the most effiecient way? once I figure out the select statement then I will switch the PHP over to MYSQLi
}
}
I am not even sure how to structure the SELECT statement efficiently which I suspect should be resulted in a while loop... what is the most efficient way to do this?