mysql查询查找具有类组合的学生

时间:2015-06-11 20:29:06

标签: mysql sql

我有一个包含2个字段的表。 Student_id和Course_id 特定的Student_ID可以参加多个课程,例如,学生2正在参加下面的示例中的课程101和102,但是学生1仅参加102个

student_id  course_id
1             102
2             101
2             102
3             303

我如何查询正在修读101和102课程的学生?或者一个学生拿102但不服用101?

可能是专家的新手问题......

1 个答案:

答案 0 :(得分:1)

这个可能吗?

Select * from tablename where (course_id = 101) OR (course_id = 102);

检查101岁而不是102岁的人

select * from tablename 
      where (course_id = 101) and 
            (student_Id not in 
                 (select Student_ID from tablename 
                                where course_id =102));

检查是否有人:

select * from tablename 
          where (course_id = 101) and 
                (student_Id in 
                     (select student_Id from tablename 
                                    where course_id =102));

SQL fiddle