如果只有值不在另一个表中,请有人建议我使用 SQL Server Select 查询从表列中获取值,这就是我想要做的:
SELECT t.TeacherID
FROM Teacher t ...... (Who are not in Class c, c.TeacherID Column)
如果只有值不在另一个表中,请有人建议我使用 SQL Server Select 查询从表列中获取值,这就是我想要做的:
SELECT t.TeacherID
FROM Teacher t ...... (Who are not in Class c, c.TeacherID Column)
select teacherID from Teacher
Except
select teacherID from Class
或者你也可以去:
select teacherID from Teacher where TeacherId not in(select teacherID from Class)
不上课的老师:
SELECT *
FROM teacher
WHERE NOT EXISTS (
SELECT *
FROM class
WHERE class.teacherid = teacher.teacherid
)
如果你想做相反的事情(找到有课的老师),那么你所要做的就是反转条件:删除NOT
!
您也可以使用 LEFT JOIN WHERE 没有为教师 ID 分配班级
`SELECT teacher.ID `
`FROM teacher LEFT JOIN Class ON teacher.ID = Class.ID`
`WHERE Class.ID IS NOT NULL`