SQL查询 - 限制结果

时间:2016-09-16 16:02:38

标签: sql

我的查询包含departmentID。我希望大约25个部门中的2个或3个部门不会返回结果。

select  i.personID, c.name, c.departmentID, se.sectionID
from individual i
INNER JOIN Section se ON se.teacherPersonID = i.personID
INNER JOIN Course c ON c.courseID = se.courseID
INNER JOIN Calendar cal ON cal.calendarID = c.calendarID and c.calendarID = 32  INNER JOIN SchoolYear sy on sy.endyear = cal.endyear and sy.active = 1
INNER JOIN activeTrial at ON at.trialID = se.trialID
inner join contact con on con.personID = i.personID
Where c.departmentID is not NULL 
order by c.departmentID

3 个答案:

答案 0 :(得分:0)

假设您不希望数据显示为部门ID的4,5和6:

WHERE c.departmentID IS NOT NULL AND c.departmentID NOT IN (4, 5, 6)

答案 1 :(得分:0)

通常,不是在where子句中进行硬编码,而是创建一个表格,例如exclusion_department,并将排除的ID存储在那里。然后在您的查询中,您将离开加入ID为NULL的表。祝你好运。

答案 2 :(得分:0)

这将有效,前提是c.departmentID是第一个键以非负值开始并递增的键。

SELECT i.personID, c.name, c.departmentID, se.sectionID
    FROM individual i
    INNER JOIN Section se ON se.teacherPersonID = i.personID
    INNER JOIN Course c ON c.courseID = se.courseID
    INNER JOIN Calendar cal ON cal.calendarID = c.calendarID 
          and c.calendarID = 32  
    INNER JOIN SchoolYear sy on sy.endyear = cal.endyear 
          and sy.active = 1
    INNER JOIN activeTrial at ON at.trialID = se.trialID
    INNER JOIN contact con on con.personID = i.personID
    WHERE IsNull(c.departmentID,-9999) NOT IN (-9999,2,3,5)
    ORDER BY c.departmentID