【问题标题】:mysql query to find students with combinations of classes [duplicate]mysql查询以查找具有班级组合的学生[重复]
【发布时间】:2015-06-11 20:29:06
【问题描述】:

我有一张有 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的学生?

对于各位专家来说可能是一个新手问题......

【问题讨论】:

    标签: mysql sql


    【解决方案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

    【讨论】:

    • 这将返回 101 或 102 中的所有学生。他要求两者中的学生,以及其中一个或另一个中的学生...
    • @JesseShellabager 编辑添加答案
    • 我认为这行不通。目标是返回 student_id 2 以查询正在学习 course_id 101 和 102 的学生。我在 [link] sqlfiddle.com/#!9/0223b7/24 上运行它,它返回的操作数应包含 1 列
    • @NandanDas 它可以工作,我编辑了它,工作正常。
    • 酷...谢谢.. 它确实有效。
    猜你喜欢
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多