2016-09-16 249 views
0

我有包含departmentID的查詢。我希望結果不予退還2個或3系出約25SQL查詢 - 限制結果

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 
+0

在SQL Server,你可以使用 「TOP(N)」 的功能,如: SELECT TOP(N)* FROM表 其中N是您想要的行數。在Oracle中,你必須添加「WHERE RowNum rualmar

+1

你能澄清一些嗎?你想要的結果是「不返回2-3分」?你有這些部門或其他數據你想刪除它們的ID? –

+1

爲什麼不試試'Where IsNull(c.departmentID,-1)不在(-1,2,3,5)'中? – AVK

回答

0

使你不希望數據顯示部門ID的4,5假設,和6:

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

通常,不要在where子句中使用硬編碼,而應創建一個表,如exclusion_department,並在其中存儲排除的ID。然後在您的查詢中,您將離開加入該表的ID爲NULL。祝你好運。

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