寻找有关以下问题的指导:
打印具有一个或多个18岁以下专业的部门的名称。

My table looks like this:
Student(sid,sname,sex,age,year,qpa)
Dept(dname,numphds)
Prof (pname,dname)
Course (cno,cname,dname)
Major(dname,sid)
Section(dname,cno,sectno,pname)
Enroll(sid,grade,dname,cno,sectno)


我最终使用以下方法获得了结果:

SELECT dept.dname
FROM dept
INNER JOIN major on major.dname = dept.dname
INNER JOIN student on major.sid = student.sid and student.age<18
group by dept.dname
having count(*)>0
limit 5;

最佳答案

您忘记了与FROM表相关的DEPT子句,并且忘记了JOIN表的第二个STUDENT(由SID链接)。另外,您需要将JOIN限制为18岁以下的学生。然后,最后,您需要具有至少1名符合年龄标准的学生的dname

SELECT DISTINCT dept.dname
FROM dept
inner join major on major.dname = dept.dname
inner join student on major.sid = student.sid and student.age < 18
group by dept.dname
having count(*)>0;

10-07 14:01