写一个查询以显示科目名称和学生姓名,这些科目名称和学生姓名在按subject_name升序排列的每个科目中获得最高分。该解决方案必须在子查询中实现。
我已使用以下查询:
select subject_name, student_name
from subject s
inner join mark m
on s.subject_id=m.subject_id
inner join student s1
on m.student_id=s1.student_id
where student_id in (select student_id
from mark
having max(value)
group by s.subject_id)
order by s.subject_name;
但是此代码在无效的关系运算符的第10行给出了错误
请帮助!
答案 0 :(得分:0)
尝试以下
SELECT
SB.SUBJECT_NAME,
S.STUDENT_NAME
FROM
STUDENT S,
SUBJECT SB,
(
SELECT
STUDENT_ID,
SUBJECT_ID,
MARKS
FROM
MARKS
WHERE
( SUBJECT_ID,
MARKS ) IN (
SELECT
M.SUBJECT_ID,
MAX(M.MARKS)
FROM
MARKS M
GROUP BY
M.SUBJECT_ID
)
) MM
WHERE
S.STUDENT_ID = MM.STUDENT_ID
AND SB.SUBJECT_ID = MM.SUBJECT_ID;
谢谢
答案 1 :(得分:0)
使用聚合函数时,必须使用子句HAVING来添加条件,因为where子句不允许使用该语法,
**SELECT columns FROM table_name WHERE condition GROUP BY columns HAVING condition**
例如存在条件的情况: max(value)> 1000
您的子查询应如下所示:
SELECT student_id, max(value)
FROM mark
WHERE student_id = s.subject_id
GROUP BY student_id
HAVING MAX(value) > 1000;
致谢!