select distinct ID, title, takes.course_id
from course join takes
on course.course_id = takes.course_id
where takes.course_id in
(select takes.course_id
from takes
where ID = '10204');
select ID, title, takes.course_id
from course join takes
on course.course_id = takes.course_id
where ID = '10204';
我要查詢的過程中ID和一個學生ID爲10204取課程名稱。第一個結果是5000行,這是不正確的。第二個給出了正確的結果。那麼第一個有什麼問題呢?爲什麼兩個查詢的結果不同?
如果'ID'是一個不明確的列名,你會得到一個錯誤。 – Thilo 2014-10-09 03:02:07
在第一個查詢中(選擇takes.course_id from where ID ='10204')這顯然是where子句中的take.ID。 第二個查詢(從課程連接中選擇ID,標題,takes.course_id參加course.course_id = takes.course_id 其中ID ='10204';)在where子句中顯然是course.ID。 你瘋了嗎? – jewelnguyen8 2014-10-09 03:12:00