1

如果只有值不在另一个表中,请有人建议我使用 SQL Server Select 查询从表列中获取值,这就是我想要做的:

SELECT t.TeacherID
FROM Teacher t ...... (Who are not in Class c, c.TeacherID Column)
4

3 回答 3

3
select teacherID from Teacher
Except
select teacherID from Class

或者你也可以去:

select teacherID from Teacher where TeacherId not in(select teacherID from Class)
于 2013-09-16T14:05:10.177 回答
1

上课的老师:

SELECT *
FROM   teacher
WHERE  NOT EXISTS (
         SELECT *
         FROM   class
         WHERE  class.teacherid = teacher.teacherid
       )

如果你想做相反的事情(找到有课的老师),那么你所要做的就是反转条件:删除NOT!

于 2013-09-16T14:06:01.713 回答
0

您也可以使用 LEFT JOIN WHERE 没有为教师 ID 分配班级

`SELECT teacher.ID `
`FROM   teacher LEFT JOIN Class ON teacher.ID =  Class.ID`
`WHERE   Class.ID IS NOT NULL`
于 2013-09-16T14:26:24.113 回答