【发布时间】:2021-06-11 06:36:44
【问题描述】:
我有两个表student和studentlastmarks,两者的架构如下:
student 表有以下列:studentid、marks。
studentlastmarks 表有:studentid、studentname、marks。
我需要在 studentlastmarks 表中获取分数大于他们最大分数的学生 ID。
例如: 以下是学生表中的数据:
studentid studentname marks
1 krishna 60
2 shiva 70
3 Arjun 50
4 Karna 65
以下是studentlastmark表中的数据
studentid marks
1 65
2 65
2 50
3 70
3 60
4 40
在本例中,我们需要返回学生 id 2 和 4 ,因为 student 在 student 表中获得的分数大于特定学生在 studentlastmarks 中获得的最大分数> 表。
我已经尝试了以下代码,它在整个 studentlast 标记表中给出了最高分,但我需要将它与 studentlastmarks 和 student 表中每个学生的最高分进行比较
select distinct s.studentid from student as s where s.marks > (select max(sl.marks) from studentlastmarks as sl)
【问题讨论】:
-
到目前为止你尝试过什么?你被困在哪里了?
-
查找最大值。在子查询中标记 studentlastmarks,然后加入并比较。
-
@shree.pat18 我试着用这个 select distinct s.studentid from student as s where s.marks > (select max(sl.marks) from studentlastmarks as sl) 它给了我有分数的学生大于整个 studentlastmark 的最大值。但我需要每个学生的最高分。
-
是否必须退回studentlastmarks中未出现的学生?
-
请注意,在现实世界中,不太可能以这种方式构造数据