SQL查询未返回任何值

时间:2013-03-08 06:08:13

标签: mysql sql

您好我有以下表结构。

Professor (EMP ID,Name,Status,Salary,Age)
Course(Course ID,Course Name,Points)
Works(Course ID,EMP ID,Class ID)

我需要做以下事情。

返回为同一班级“10级”选择了2门不同课程M1和M2的员工名单

这是我写的查询。

SELECT p.EmpID, p.Name, p.Status, p.Salary 
FROM professor p, course c, works w 
WHERE p.EmpID = w.EmpID
AND
w.CourseID = c.CourseID
AND
w.ClassID = 10
AND
c.CourseName IN ( SELECT CourseName FROM course WHERE CourseName = 'm1'
AND CourseName = 'm2')

但即使数据库中有数据,查询也不会返回任何值。

2 个答案:

答案 0 :(得分:2)

此问题通常称为Relational Division

SELECT  a.EmpID, a.name
FROM    Professor a
        INNER JOIN Works b
            ON a.EmpID = b.EmpID AND b.ClassID = 10 
        INNER JOIN  Course c
            ON b.CourseID = c.CourseID
WHERE   c.CourseNAME IN ('M1', 'M2')
GROUP   BY a.EmpID, a.name
HAVING  COUNT(DISTINCT c.CourseNAME) = 2

答案 1 :(得分:1)

子查询

( SELECT CourseName FROM course WHERE CourseName = 'm1' AND CourseName = 'm2')

什么都不会返回。看看“AND”