My current tables look like this:
Lessons:
+-----------------------------------+
| ID | Name | StartDate | Repeats |
|----|-------|------------|---------|
| 1 | Maths | 2014-05-05 | 5 |
| 2 | Lunch | 2014-05-05 | 1 |
| 3 | Comp | 2014-05-05 | 7 |
+-----------------------------------+
LessonTimes:
+-------------------------------------+
| ID | LessonID | StartTime | EndTime |
|----|----------|-----------|---------|
| 1 | 1 | 10:00:00 | 5 |
| 2 | 2 | 12:25:00 | 1 |
| 3 | 3 | 14:00:00 | 7 |
+-------------------------------------+
Tally:
+----+
| ID |
|----|
| 1 |
| 2 |
| . |
| . |
+----+
I have events that repeat on a certain number of days with a specific start date. The current query I have is:
SELECT E.ID
, E.Name
, E.StartDate
, E.Repeats
, A.ShowDate
, DATEDIFF(E.StartDate, A.ShowDate) diff
, T.StartTime
, DATE_ADD(A.ShowDate, INTERVAL T.StartTime HOUR_SECOND) ShowTime
FROM Planner_Lessons E
, ( SELECT DATE_ADD('2014-05-05 00:00:00',INTERVAL ID DAY ) ShowDate
FROM `Planner_Tally`
WHERE (DATE_ADD('2014-05-05 00:00:00',INTERVAL ID DAY )<= '2014-05-30 00:00:00')
ORDER
BY Id ASC
) A
LEFT
JOIN Planner_LessonTimes T
ON T.LessonID = E.ID
WHERE MOD(DATEDIFF(E.StartDate, A.ShowDate), E.Repeats) = 0
AND A.ShowDate >= E.StartDate
But the error I get is saying that the field E.ID
cannot be found in the "ON" clause.
The original question I found the query on is here - PHP/MySQL: Model repeating events in a database but query for date ranges