【问题标题】:How do I create a query that list the name of students who took fewer than three classes or have a null value for class如何创建一个查询,列出参加少于三门课程或课程为空值的学生的姓名
【发布时间】:2017-12-04 05:56:59
【问题描述】:

我有一个“数据库”,一个学生有两张表,然后注册。学生表有 stuid、lastname、firstname。登记表有 stuid 班级编号和年级。

我必须返回一个看起来像这样的列表


伯恩斯 琼斯 李 麦卡锡 里维拉 史密斯

名字

爱德华 玛丽 佩里 欧文 简 汤姆

我试图做这样的声明

Select lastname, firstname Where Not exists (Select stuid from enroll e where e.stuid = s.stuid) Having count(*) < 3 Group By lastname, firstname;

我知道我基本上需要两个合并这两个表,以便我得到班级

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    使用LEFT JOINHAVING

     SELECT st.stuid, lastname, firstname 
     FROM student st 
     LEFT JOIN enroll el ON st.stuid = el.stuid 
     GROUP BY st.stuid 
     HAVING count(el.stuid) < 3
    

    【讨论】:

      【解决方案2】:

      试试这样:

      SELECT lastname, firstname FROM student st LEFT JOIN enroll el 
      ON st.stuid = el.stid 
      WHERE (el.classnumber < 3 OR el.classnumber IS NULL) 
      

      【讨论】:

        【解决方案3】:

        您的“where 子句”需要在您的“from 子句”之后。那就试试吧!

         select firstname , lastname
         from student s join enroll e 
         on s.stuid = e.stuid
         Where e.classnumber < 3 and e.classnumber is null 
        

        【讨论】:

          猜你喜欢
          • 2015-02-09
          • 2014-06-14
          • 2021-11-28
          • 1970-01-01
          • 2022-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多