2017-08-10 72 views
0

我有兩個表StudentProgressSongs。我想選擇所有結果,其中可以加入從SongsStudentProgress,並沒有兩個表連接,也StudentIDStudentProgress所有結果有一個特定的值..選擇哪裏id不存在,它的值是'特定值'

這是我爲我的第一個表的代碼效果很好

select 
    s.SongID, 
    s.Title, 
    s.Location, 
    s.Rhythm 
from 
    Songs s 
join 
    StudentProgress f on s.SongID = f.SongID 
where 
    f.StudentID = 17 

現在我想從StudentProgres表,該表沒有在Songs表中存在,也有特定的StudentID值相同的選擇。

我想這樣

select 
    s.SongID, 
    s.Title, 
    s.Location, 
    s.Rhythm 
from 
    Songs s 
join 
    StudentProgress f on s.SongID = f.SongID 
where 
    f.StudentID is null 
    and f.studentid = 17 

但我沒有得到任何結果。

在這裏看到的截圖來了解

enter image description here

+1

第二個查詢中的WHERE子句無效。 'f.StudentID'不可能是NULL和17. –

+0

我需要他們兩個。我的意思是studentid = 17而不存在表歌曲 – user6453020

+0

當你正在尋找歌曲表中不存在的東西時,你不能從's.SongID'之類的's'項目返回項目。你的問題陳述是反向的嗎? – NetMage

回答

0

使用left Join使用的連接條件,你可以在條件f.studentid = 17找到哪首歌沒有學生

select s.SongID, 
     s.Title, 
     s.Location, 
     s.Rhythm 
    from Songs s 
    left join StudentProgress f 
    on s.SongID = f.SongID 
    and f.studentid = 17 
where f.StudentID is null 
+0

非常感謝!這是我正在尋找的 – user6453020

0

以找到那些沒有帶學生證17相關Songs

select s.SongID, 
     s.Title, 
     s.Location, 
     s.Rhythm 
    from Songs s 
    where s.SongID not in (select f.SongId from StudentProgress f where f.StudentID = 17)