我编写了以下MySQL查询,该查询成功返回了一个主题列表以及截至特定日期的最新值:
SELECT t1.subject, t1.judgement AS judgement1, t1.assessor AS assessor1, t1.dateAchieved AS dateAchieved1
FROM `wwlpredictedoveralljudgements` t1
WHERE pupilID='115137218853' && t1.dateAchieved = (
SELECT MAX(t2.dateAchieved)
FROM `wwlpredictedoveralljudgements` t2
WHERE t1.pupilID = t2.pupilID && t1.subject=t2.subject && dateAchieved<='2018-01-25 00:00:00')
它完全按照我的需要工作。我想做的是另外3列: 判断2,评估者2,日期成立2 从第2个指定日期开始。 我已经阅读并尝试了各种连接和子查询,但我没有设法获得工作结果。
结果如下:
我需要额外的3列,但在第二个MAX选择中有第二个截止日期。有没有人有任何想法如何做到这一点?
答案 0 :(得分:0)
解决。具有类似问题的任何其他人的解决方案...左连接返回到具有第二个WHERE条件的同一个表。
SELECT t1.pupilID, t1.subject, t1.judgement AS judgement1, t1.assessor AS assessor1, t1.dateAchieved AS dateAchieved1 , t3.judgement AS judgement2, t3.assessor AS assessor2, t3.dateAchieved AS dateAchieved2
FROM `wwlpredictedoveralljudgements` t1
LEFT JOIN wwlpredictedoveralljudgements t3 ON t3.pupilID=t1.pupilID && t3.subject=t1.subject
WHERE t1.pupilID='084601821376' && t1.dateAchieved = (
SELECT MAX(t2.dateAchieved) FROM `wwlpredictedoveralljudgements` t2
WHERE t1.pupilID = t2.pupilID && t1.subject=t2.subject && dateAchieved<='2018-01-25 00:00:00') && t3.dateAchieved = (
SELECT MAX(t4.dateAchieved)
FROM `wwlpredictedoveralljudgements` t4 WHERE t3.pupilID = t4.pupilID && t3.subject=t4.subject && dateAchieved<='2018-03-25 00:00:00')