2015-12-02 74 views
1

我有兩個表:嵌套查詢不執行如預期

Catalog(ID, CourseName, PreReq, Professor) 
PreviousCourses(Username, PreviousCoursesID, Grade) 

PreviousCoursesID對應ID的目錄。我正在嘗試使用這個關係來訪問CourseName。

這裏是我的查詢:

Select CourseName from Catalog where ID = (Select PrevCoursesID from PreviousCourses where Username = 'admin') 

它執行了,但返回一個空集。

Select PrevCoursesID from PreviousCourses where Username = 'admin'查詢本身返回PrevCoursesID,所以我不明白爲什麼我的嵌套查詢不起作用。

回答

0
Select CourseName from Catalog where ID = (Select PrevCoursesID from PreviousCourses where Username = 'admin') 

需要使用「IN」,而不是「=」這樣

Select CourseName from Catalog where ID IN (Select PrevCoursesID from PreviousCourses where Username = 'admin') 

因爲它是一組值,而不是一個單一的值的。當檢查多個值時,您需要使用IN關鍵字。這將是相同的

Select CourseName from Catalog where ID = (1,2,3,4,5) 
+0

是的,我已經試過,以及。它返回空集 – StephCurry3093

+0

什麼**從PreviousCourses中選擇PrevCoursesID,其中Username ='admin'** return? – Spasticmonkey

+0

10000.它返回PrevCoursesID數據,其中用戶名是admin – StephCurry3093