我目前的表格如下:
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 |
| . |
| . |
+----+
我的活动会在特定天数内以特定的开始日期重复。我当前的查询是:
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
但我得到的错误是在“ON”子句中找不到字段E.ID
。
我发现查询的原始问题在这里 - PHP/MySQL: Model repeating events in a database but query for date ranges
答案 0 :(得分:0)
这是您的查询格式,以便人们可以阅读:
SELECT E.ID, E.Name, E.StartDate, E.Repeats, A.ShowDate, DATEDIFF(E.StartDate, A.ShowDate) AS diff,
T.StartTime, DATE_ADD(A.ShowDate, INTERVAL T.StartTime HOUR_SECOND) AS ShowTime
FROM Planner_Lessons AS E,
(SELECT DATE_ADD('2014-05-05 00:00:00',INTERVAL ID DAY) as 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 AS T
ON T.LessonID=E.ID
WHERE MOD(DATEDIFF(E.StartDate, A.ShowDate), E.Repeats)=0 AND A.ShowDate>=E.StartDate;
您缺少隐式和显式join
语法。由于MySQL的作用域规则,逗号后无法识别E
中的列。
SELECT E.ID, E.Name, E.StartDate, E.Repeats, A.ShowDate, DATEDIFF(E.StartDate, A.ShowDate) AS diff,
T.StartTime, DATE_ADD(A.ShowDate, INTERVAL T.StartTime HOUR_SECOND) AS ShowTime
FROM Planner_Lessons E JOIN
Planner_LessonTimes T
ON T.LessonID = E.ID JOIN
(SELECT DATE_ADD('2014-05-05 00:00:00',INTERVAL ID DAY) as 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
ON MOD(DATEDIFF(E.StartDate, A.ShowDate), E.Repeats)=0 AND A.ShowDate>=E.StartDate;
我将left join
切换为内连接,因为where
子句撤消了外连接。
答案 1 :(得分:0)
您错过了子查询别名为 A 的JOIN条件。假设Planner_Tally
表包含列ID
,您可以在A.ID=E.ID
添加加入条件,如下所示
SELECT E.ID, E.Name, E.StartDate, E.Repeats, A.ShowDate,
DATEDIFF(E.StartDate, A.ShowDate) AS diff, T.StartTime,
DATE_ADD(A.ShowDate, INTERVAL T.StartTime HOUR_SECOND) AS ShowTime
FROM Planner_Lessons AS E
LEFT JOIN Planner_LessonTimes AS T ON T.LessonID=E.ID
LEFT JOIN (
SELECT DATE_ADD('2014-05-05 00:00:00',INTERVAL ID DAY) as ShowDate,ID
FROM `Planner_Tally`
WHERE (DATE_ADD('2014-05-05 00:00:00',INTERVAL ID DAY)<='2014-05-30 00:00:00')
) A ON A.ID=E.ID
WHERE MOD(DATEDIFF(E.StartDate, A.ShowDate), E.Repeats)=0
AND A.ShowDate>=E.StartDate
ORDER BY E.Id ASC
答案 2 :(得分:0)
顺便说一句,请考虑以下内容......(ints
是0-9的整数表)
EXPLAIN
SELECT * FROM ints WHERE '2014-05-05 00:00:00' + INTERVAL i DAY < '2014-30-30 00:00:00'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: ints
type: index
possible_keys: NULL
key: PRIMARY
key_len: 4
ref: NULL
rows: 10
Extra: Using where; Using index
1 row in set (0.00 sec)
EXPLAIN
SELECT * FROM ints WHERE i < DATEDIFF('2014-05-30 00:00:00','2014-05-05 00:00:00')\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: ints
type: index
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: NULL
rows: 10
Extra: Using where; Using index
1 row in set (0.00 sec)
正如您所看到的,虽然两个查询在逻辑上是相同的,但第一个查询为possible_keys
注册NULL。
我们现在已经达到了我对指数的全面了解。