找出那些已经重修了3门不同课程的ID。

问题描述 投票:0回答:1

(我是新来的,如果这太简单的话,请提前道歉)所以,我想找到那些至少重修过三门不同课程的学生,至少一次.我想,我将不得不对Takes和学生进行操作。

Takes(ID,course_id,sec_id,semester,year,grade)
student(ID,name,dept_name,tot_cred)

这两个ID作为主键

下面是我研究的两个解决方案。

1

select student.*
from student, takes
where student.ID = takes.ID 
group by takes.course_id
having count(distinct takes.course_id) <=3 ;

2

select student.*
from student
where 2<= (select takes.ID from takes
        where takes.ID = student.ID 
        group by course_id
        having count(distinct takes.course_id) <=3
        limit 1);

我不得不加了那个限制器,因为一直出现错误。

mysql sql
1个回答
-1
投票

从来没有 用逗号 FROM 句。 一定要用恰当的、明确的。标准,可读 JOIN 句法。

澄清:重修在这里的意思是;该学生应该至少修过两次课程)

如果你想让学生至少选了三次单门课程,那么就使用 GROUP BYHAVING:

select t.student_id, t.course_id
from takes t
group by t.student_id, t.course_id
having count(*) >= 3;

您可以使用 JOININEXISTS 带来更多的栏目,从 students 如果你喜欢。

© www.soinside.com 2019 - 2024. All rights reserved.