有3张表,Student表、SC表和Course表 
Student表:学号(Sno)、姓名(Sname)、性别(Ssex)、年龄(Sage)和系名(Sdept) 
Course表:课程号(Cno)、课程名(Cname)和学分(Ccredit); 
SC表:学号(Sno)、课程号(Cno)和成绩(Grade) 
请使用SQL语句查询学生姓名及其课程总学分 
(注:如果课程不及格,那么此课程学分为0)

解决方案 »

  1.   

    oracle:select sname,sum(ccredit) from(select st.sname as sname,c.cname as cname,(case when s.grade >=60 then c.ccredit else 0 end) as ccredit
    from student st,course c,sc s
    where st.sno=s.sno and s.cno=c.cno) t group by t.sname
      

  2.   

    select s.Sname,sum(c.ccredit) from course c,student s  where exists(select * from sc where grade>60 and sc.sno=s.sno and sc.cno=c.cno)
      

  3.   

    符合sql-92标准写法,sql-server oracle通用
    select s.sno,s.sname,sum(case when sc.grade>0 then c.ccredit else 0 end) totalcredit
    from 
    student s left join sc on s.studentid = sc.studentid
    left join course c on sc.courseid = c.courseid
    group by s.sno,s.sname
      

  4.   

    select s.sname,(select sum(c.ccredit) from course c  where c.cno in (select sc.cno from sc where grade>60 and sc.sno=s.sno) ) from student s
      

  5.   

    select s.sno,s.sname,sum(case when sc.grade>0 then c.ccredit else 0 end) totalcredit
    from  student s left join sc on s.studentid = sc.studentid
    left join course c on sc.courseid = c.courseid
    group by s.sno,s.sname
      

  6.   

    select a.Sname, sum(case when c.Ccredit is null then 0 else c.Ccredit end) as totalCcredit 
    from Student a
    left join SC b on a.Sno = b.Sno
    join Course c on b.Cno = c.Cno
    where b.Grade >= 60
    group by a.Sno, a.Sname不用 left join 容易丢人,group by 没学号遇到重名就惨了。
      

  7.   

    select s.sname,sum(ccredit) from student s,course c,sc where s.sno=sc.sno and sc.cno=c.cno and grade>=60 group by a.sno写法还真多学习了!!
      

  8.   

    sql比较有意思,怎么写都可以实现。。