MySQL在正确的位置添加Where子句

时间:2014-05-06 20:31:25

标签: mysql sql where-clause

我的查询:

SELECT E.ID
     , E.Name
     , E.StartDate
     , E.Repeats
     , A.ShowDate
     , DATE_ADD(DATE_ADD(A.ShowDate, INTERVAL (T.Day-1) DAY), INTERVAL T.StartTime HOUR_SECOND) AS NewShowDate
     , (T.Day-1) AS NewDay
FROM Planner_Lessons E JOIN
 Planner_LessonTimesNew T
 ON T.LessonID = E.ID INNER JOIN Planner_Sessions S ON S.UserID=T.UserID JOIN
 (SELECT DATE_ADD(NOW(),INTERVAL ID DAY) as ShowDate
  FROM `Planner_Tally`
  ORDER BY ID ASC
 ) A 
 ON MOD(DATEDIFF(E.StartDate, A.ShowDate), E.Repeats)=0 AND A.ShowDate>=E.StartDate AND E.Deleted='No' AND T.Deleted='No' WHERE S.SID='1234' AND NewShowDate<='2014-05-13 00:00:00' ORDER BY NewShowDate ASC

但是where子句NewShowDate<='2014-05-13 00:00:00'导致了一个问题,因为MySQL说无法找到NewShowData。除了答案,我想解释出现了什么问题以及如何解决它。我是MySQL的新手,希望更详细地学习它,以帮助我自己解决未来的问题。

3 个答案:

答案 0 :(得分:0)

使用派生子查询

SELECT * FROM
(
    SELECT E.ID
         , E.Name
         , E.StartDate
         , E.Repeats
         , A.ShowDate
         , DATE_ADD(DATE_ADD(A.ShowDate, INTERVAL (T.Day-1) DAY), INTERVAL T.StartTime HOUR_SECOND) AS NewShowDate
         , (T.Day-1) AS NewDay
    FROM Planner_Lessons E JOIN
     Planner_LessonTimesNew T
     ON T.LessonID = E.ID INNER JOIN Planner_Sessions S ON S.UserID=T.UserID JOIN
     (SELECT DATE_ADD(NOW(),INTERVAL ID DAY) as ShowDate
      FROM `Planner_Tally`
      ORDER BY ID ASC
     ) A 
     ON MOD(DATEDIFF(E.StartDate, A.ShowDate), E.Repeats)=0 AND A.ShowDate>=E.StartDate AND E.Deleted='No' AND T.Deleted='No' 
)As Z
WHERE Z.SID='1234' AND Z.NewShowDate<='2014-05-13 00:00:00' ORDER BY Z.NewShowDate ASC

答案 1 :(得分:0)

由于在执行WHERE子句后生成了最终结果集(因此也是别名),您可以将查询包装在子查询中

SELECT * 
FROM
(
SELECT E.ID
     , E.Name
     , E.StartDate
     , E.Repeats
     , A.ShowDate
     , DATE_ADD(DATE_ADD(A.ShowDate, INTERVAL (T.Day-1) DAY), INTERVAL T.StartTime HOUR_SECOND) AS NewShowDate
     , (T.Day-1) AS NewDay
FROM Planner_Lessons E JOIN
 Planner_LessonTimesNew T
 ON T.LessonID = E.ID INNER JOIN Planner_Sessions S ON S.UserID=T.UserID JOIN
 (SELECT DATE_ADD(NOW(),INTERVAL ID DAY) as ShowDate
  FROM `Planner_Tally`
  ORDER BY ID ASC
 ) A 
 ON MOD(DATEDIFF(E.StartDate, A.ShowDate), E.Repeats)=0 AND A.ShowDate>=E.StartDate AND E.Deleted='No' AND T.Deleted='No'
WHERE S.SID='1234'
) A
WHERE NewShowDate<='2014-05-13 00:00:00'
ORDER BY NewShowDate ASC

WHERE子句中的引用表达式。

SELECT E.ID
     , E.Name
     , E.StartDate
     , E.Repeats
     , A.ShowDate
     , DATE_ADD(DATE_ADD(A.ShowDate, INTERVAL (T.Day-1) DAY), INTERVAL T.StartTime HOUR_SECOND) AS NewShowDate
     , (T.Day-1) AS NewDay
FROM Planner_Lessons E JOIN
 Planner_LessonTimesNew T
 ON T.LessonID = E.ID INNER JOIN Planner_Sessions S ON S.UserID=T.UserID JOIN
 (SELECT DATE_ADD(NOW(),INTERVAL ID DAY) as ShowDate
  FROM `Planner_Tally`
  ORDER BY ID ASC
 ) A 
 ON MOD(DATEDIFF(E.StartDate, A.ShowDate), E.Repeats)=0 AND A.ShowDate>=E.StartDate AND E.Deleted='No' AND T.Deleted='No'
WHERE S.SID='1234'
  AND DATE_ADD(DATE_ADD(A.ShowDate, INTERVAL (T.Day-1) DAY), INTERVAL T.StartTime HOUR_SECOND) <= '2014-05-13 00:00:00'
ORDER BY NewShowDate ASC

答案 2 :(得分:0)

使用HAVING过滤NewShowDate,where子句不会过滤基于自定义别名的列的记录,也不会过滤使用HAVING子句过滤它们的聚合函数的结果

SELECT E.ID
     , E.Name
     , E.StartDate
     , E.Repeats
     , A.ShowDate
     , DATE_ADD(DATE_ADD(A.ShowDate, INTERVAL (T.Day-1) DAY), INTERVAL T.StartTime HOUR_SECOND) AS NewShowDate
     , (T.Day-1) AS NewDay
FROM Planner_Lessons E JOIN
 Planner_LessonTimesNew T
 ON T.LessonID = E.ID INNER JOIN Planner_Sessions S ON S.UserID=T.UserID JOIN
 (SELECT DATE_ADD(NOW(),INTERVAL ID DAY) as ShowDate
  FROM `Planner_Tally`
  ORDER BY ID ASC
 ) A 
 ON MOD(DATEDIFF(E.StartDate, A.ShowDate), E.Repeats)=0
 AND A.ShowDate>=E.StartDate AND E.Deleted='No' AND T.Deleted='No'
 WHERE S.SID='1234'
 HAVING NewShowDate<='2014-05-13 00:00:00' 
ORDER BY NewShowDate ASC