2017-08-10 64 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)