我有兩個表:指定別名子查詢的MySQL表
- 的
exam
表包含學生 - 和學生表進行評分
兩者都加入了這個方式:student.id = exam.student_id。
我試圖讓firt五個學生誰在使用下面的查詢超過5天的平均他們的得分計算出的最高分:
SELECT
student.id as std_id,
student.name,
(SELECT AVG(score) FROM exam WHERE exam.student_id=std_id ORDER BY exam.timestamp DESC LIMIT 5) AS score
FROM student
ORDER BY score
DESC LIMIT 5
我有以下錯誤:
#1054 - Unknown column 'std_id' in 'where clause'
我也嘗試過用student.id替換std_id,但仍然沒有運氣。
任何想法如何解決這個問題?非常感謝
------------------------------------------------------------------------------
對不起,我犯了我的邏輯錯誤。 如前所述,平均值計算爲
only the last 5 scores recorded
。 更新查詢:
SELECT
student.id as std_id,
student.name,
(SELECT AVG(score) FROM (SELECT score FROM exam WHERE exam.student_id=student.id ORDER BY exam.timestamp DESC LIMIT 5) AS score) AS score
FROM student
ORDER BY score
DESC LIMIT 5
這是給錯誤的地方是我設置exam.student_id = student.id
感謝。
你可以發佈你的表定義和一些示例數據嗎? – Kickstart