班级表
ID(主键),CLASSID(班号), GRADE(年级)
教师-班级对应表
ID(主键),CLASSID(对应班级表的ID),TEACHERID(对应教师表的ID)怎么直接用sql语句就能判断  某一年级的所有班都至少有一名授课教师?

解决方案 »

  1.   


    COUNT(TEACHERID) GROUP BY CLASSID;根据CLASSID聚合之后,判断TEACHER的数量<1就好了
      

  2.   

    select a.classid,count(teacherid) 
    from class a,teacher b
    where a.classid=b.classid(+)
    group by a.classid having count(teacherid)>0
      

  3.   

    楼上的SQL只能判断出某一个班级有几名授课老师。但不知道某一年级的所有班是否都有授课老师。
    楼主要求:某一年级的所有班都至少有一名授课教师。
    将SQL优化,下面这个SQL返回某一个班级没有授课老师的年级。
    将cn改成>0则返回有授课老师的年级.
    select distinct grade "某一个班没有授课老师的年级" from (
    select a.grade grade, a.classid ci, count(teacherid) cn
    from class a,teacher b
    where a.classid=b.classid(+)
    group by a.grade, a.classid ) where cn = 0
      

  4.   

    第一步:
    通过classid关联,得到年级和教师id对应的子查询
    第二步:
    对子查询进行按照年级进行count(distinct 教师id),在having 字句中用 count(distinct 教师id)>0
      

  5.   

    select * from 班级表 where CLASSID(班号) not in
    (select CLASSID from 教师-班级对应表)