I have Problems with a MySQL query. I have the following table:
[id]
[from] DATE (Y-m-d)
[days] INT
[...]
and i have the two PHP variables $start and $end. I want to have this result for example:
$start = 2012-19-03
$end = 2012-24-03
$result = array(
[0] => COUNT of rows WHERE '2012-19-03' BETWEEN from AND from + INTERVAL days DAY
[1] => COUNT of rows WHERE '2012-20-03' BETWEEN from AND from + INTERVAL days DAY
[2] => COUNT of rows WHERE '2012-21-03' BETWEEN from AND from + INTERVAL days DAY
[4] => COUNT of rows WHERE '2012-22-03' BETWEEN from AND from + INTERVAL days DAY
[5] => COUNT of rows WHERE '2012-23-03' BETWEEN from AND from + INTERVAL days DAY
[6] => COUNT of rows WHERE '2012-24-03' BETWEEN from AND from + INTERVAL days DAY
}
Till now I was doing this in a while loop with a query for each day but there should be a better way. My Problem is the GROUP BY, where the days that not exist in the data.
More detailed example:
Sample data:
id, from, days
1, 2012-15-03, 2
2, 2012-15-03, 5
3, 2012-13-03, 20
4, 2012-16-03, 1
Desired result:
$start = 2012-11-03
$end = 2012-19-03
$result = array(
[0] => 0 (ROWS IN DB WHERE '2012-11-03' BETWEEN from AND from+days -- no match in data),
[1] => 0 (ROWS IN DB WHERE '2012-12-03' BETWEEN from AND from+days -- no match in data),
[2] => 1 (ROWS IN DB WHERE '2012-13-03' BETWEEN from AND from+days -- id #3 matches),
[3] => 1 (and so on),
[4] => 3,
[5] => 4,
[6] => 3,
[7] => 2
)
like this..
hing hours without finding a way I hope you can help me, Greetings