【发布时间】:2012-07-18 22:38:09
【问题描述】:
我有一个数据结构,其中学生和小组之间存在多对多关系。我有三张桌子
学生:身份证、姓名
组:ID、名称
students_groups:student_id、group_id
如何只选择不属于特定组的学生(例如 group.id = 1)?
我做了一些搜索并尝试使用子查询但只得到一个空集...
select * from students where not exists (select students.* from students left join students_groups on students_groups.student_id = student.id where students_groups.group_id = 1);
我应该如何查询?提前谢谢!
编辑 好的,看来以下两个终于可以工作了...有人可以向我解释为什么我不需要加入表格就可以工作吗???
select * from students where not exists (select * from students_groups where students_groups.student_id = student.id and student_groups.group_id = 1);
select * from students where id not in (select student_id from students_groups where group_id = 1);
【问题讨论】: