使用studentID,studentScore,科目分数
假设表分数有此
1 40 maths
3 20 english
2 23 english
1 30 english
4 10 maths
一个名为students的表,其中包含studentID,studentName
假设表学生有这个
1 John Smith
2 anne grey
3 bob lemons
4 chris something
我想要学生的名字,分数,所有分数低于31的科目,所以约翰史密斯有1分超过31分(40分),所以他不计入结果
应该是这个结果
bob lemons 20 english
anne grey 23 english
chris something 10 maths
干杯。
答案 0 :(得分:1)
您可以尝试以下操作:
select st.studentName
, sc.score
, sc.subject
from students st
join scores sc on sc.studentID = st.studentID
join (
select studentID
from scores
group by studentID
having max(studentScore) < 31
) t2 on t2.studentID = st.studentID
概念如下:如果学生在一组分数中得分高于31,则我们将该组排除在结果集之外。
答案 1 :(得分:1)
试试这个
SELECT s.studentName, sc.score, sc.subject
FROM students s JOIN scores sc ON sc.studentID = s.studentID
WHERE NOT EXISTS ( SELECT 'a'
FROM scores ss
WHERE ss.studentID = s.studentID
AND ss.studentScore > 30
)