(我是新来的,如果这太简单的话,请提前道歉)所以,我想找到那些至少重修过三门不同课程的学生,至少一次.我想,我将不得不对Takes和学生进行操作。
Takes(ID,course_id,sec_id,semester,year,grade)
student(ID,name,dept_name,tot_cred)
这两个ID作为主键
下面是我研究的两个解决方案。
select student.*
from student, takes
where student.ID = takes.ID
group by takes.course_id
having count(distinct takes.course_id) <=3 ;
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);
我不得不加了那个限制器,因为一直出现错误。
从来没有 用逗号 FROM
句。 一定要用恰当的、明确的。标准,可读 JOIN
句法。
澄清:重修在这里的意思是;该学生应该至少修过两次课程)
如果你想让学生至少选了三次单门课程,那么就使用 GROUP BY
和 HAVING
:
select t.student_id, t.course_id
from takes t
group by t.student_id, t.course_id
having count(*) >= 3;
您可以使用 JOIN
或 IN
或 EXISTS
带来更多的栏目,从 students
如果你喜欢。