MySQL:多个选择和连接查询

时间:2015-12-05 13:06:13

标签: mysql sql

我有以下架构:

Department (dept_id, dept_name)

Student (student_id, student_name, major, level, age)

Professor (prof_id, prof_name, dept_id)

Class (name, meets_at, room, prof_id)

Enrolled (student_id, class_name, semester)

我需要查找在课程中注册的级别为SR的学生的姓名 由在Computer Science部门工作的教授讲授。

根据我的关系代数理解,我的尝试是

SELECT s.student_name FROM ( SELECT d.dept_id FROM department as d WHERE d.dept_name = 'computer science' 

                        JOIN professor as p on d.dept_id = p.dept_id

                        JOIN class as c on p.prof_id = c.prof_id 
                        JOIN enrolled as e on c.name = e.class_name
                        JOIN student as s on s.student_id = e.student_id )
                        WHERE s.level = 'sr' ;

但是当我尝试它时,我得到sql语法错误。 任何帮助深表感谢。

2 个答案:

答案 0 :(得分:0)

SELECT s.student_name 
FROM department d
JOIN professor p on d.dept_id = p.dept_id
JOIN class c on p.prof_id = c.prof_id 
JOIN enrolled e on c.name = e.class_name
JOIN student s on s.student_id = e.student_id 
WHERE s.level = 'sr' 
    AND d.dept_name = 'computer science' 

答案 1 :(得分:0)

SELECT s.student_name FROM(你必须提及表名),其中s.level ='sr';

没有表名,就会显示错误。