查询输出中缺少日期

时间:2016-08-22 14:25:12

标签: mysql coldfusion

我在Windows平台上使用cf 9

我可以运行一个SQL语句,其中我的30天缺席的学生将出现在此输出列表中。我遇到问题的地方是,每个教师旁边都要向学生展示最后一次出勤日期。当我运行查询时,我不知何故出现了这个问题。我使用的查询如下:

SELECT Stuid,
       TRIM(CONCAT(name.fname,' ',name.lname)) AS OVER_30_Days,
       ltfname AS instructor,
       MAX(meedate)
FROM name
LEFT JOIN meeting
    ON meeStuid = Stuid AND
       meedate BETWEEN NOW() - INTERVAL 31 DAY AND NOW()
LEFT JOIN squadlt
    ON squadlt = ltid
WHERE meeStuid IS NULL AND
      type = 'pupil' AND
      city = 'jackson' AND
      status = 'a' AND
      Stuid <> '432' AND
      Stuid <> '5' AND
      Stuid <> '183'
ORDER BY ltfname, OVER_30_Days

下面的结果列表在maxdate列中显示为null值。有什么我做错了吗?

Stuid     OVER_30_Days      Instructor      Max(meedate)
445       Jerry Biden       Dr. Richards    Null
446       Kenny Gold        Dr. Sondra      Null
444       Timothy Crim      Dr. Kumar       Null

1 个答案:

答案 0 :(得分:1)

您可以将您的直接加入替换为meeting表格,并加入子查询,该子查询使用GROUP BY聚合来查找过去30天内每个学生的最后出勤日期。这是完整的查询,您会注意到我所做的就是将直接替换为meeting

SELECT Stuid,
       TRIM(CONCAT(name.fname,' ',name.lname)) AS OVER_30_Days,
       ltfname AS instructor,
       t.meedate
FROM name
LEFT JOIN
(
    SELECT meeStuid, MAX(meedate) AS meedate
    FROM meeting
    WHERE meedate BETWEEN NOW() - INTERVAL 31 DAY AND NOW()
    GROUP BY meeStuid
) t
    ON t.meeStuid = Stuid
LEFT JOIN squadlt
    ON squadlt = ltid
WHERE meeStuid IS NULL AND
      type = 'pupil' AND
      city = 'jackson' AND
      status = 'a' AND
      Stuid <> '432' AND
      Stuid <> '5' AND
      Stuid <> '183'
ORDER BY ltfname, OVER_30_Days