选择部门名称查询先决条件

时间:2014-02-06 08:20:30

标签: sql sql-server sql-server-2008 tsql

我有三张表,如下所示:

Courses(
    Number: integer, 
    DeptName: string, 
    CourseName: string,
    Classroom: string, 
    Enrollment: integer)

Departments(
    Name: string, 
    ChairmanPID: string)

PreReq(
    Number: integer, 
    DeptName: string, 
    PreReqNumber: integer,
    PreReqDeptName: string)

我必须找到哪些部门的课程在其他部门有先决条件?

2 个答案:

答案 0 :(得分:1)

试试这个

Select D.Name,C.CourseName, P.DeptName 
     From Department D
     INNER JOIN Courses C ON C.DeptName=D.DeptName
     INNER JOIN PreReq P ON P.PreReqDeptName=D.DeptName AND P.DeptName <> D.DeptName

答案 1 :(得分:0)

SELECT DISTINCT p.DeptName 
FROM PreReq p 
WHERE p.DeptName <> p.PreReqDeptName