我有两张桌子:
exam
包含学生成绩的表格- 和学生表
两者都以这种方式加入:student.id=exam.student_id。
我正在尝试使用以下查询获得前五名得分最高的学生,这些学生通过 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,但仍然没有运气。
知道如何解决这个问题吗?十分感谢
------------------------------------------------------------------------------
对不起,我在逻辑上犯了一个错误。如前所述,平均值计算为
仅记录最后 5 个分数
. 更新查询:
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
谢谢。