选择ID不存在的位置,其值为'特定值'

时间:2017-08-10 20:43:10

标签: sql sql-server-2008

我有两张表StudentProgressSongs。我想选择可以从Songs加入StudentProgress的所有结果,并且所有未加入两个表格以及StudentID StudentProgress的结果都具有特定值。

这是我的第一张表格的代码

 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

2 个答案:

答案 0 :(得分:0)

要查找与学生ID 17无关的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)

@Dao
interface GroupDao {

    @Insert
    fun insert(group: Group)

    @Query("SELECT * FROM group")
    fun loadAll(): LiveData<List<Group>>
}

答案 1 :(得分:0)

在加入条件下使用条件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 ,您可以找到哪首歌没有学生

@Override
public DriveStatsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_layout, viewGroup, false);
    DriveStatsViewHolder dsvh = new DriveStatsViewHolder(v);
    return  dsvh;
}