MySQL常用操作

S表:C表:

 

SC表:

SC_C表:

 

操作语句:

-- 选出每门课程的课程号,课程名称和平均分,插入到SC_C表中
INSERT INTO sc_c(CNO,CNAME,AVG_GRADE)
SELECT SC.CNO,C.CNAME,AVG(SC.SCGRADE) FROM SC,C WHERE SC.CNO=C.CNO GROUP BY CNO;

-- 把选了张学友老师所授课程的女生的选课记录删除
delete SC from SC,s,C where SC.CNO=C.CNO and SC.SNO=s.SNO and s.SEX='' and C.CTEACHER='张学友';

-- 找出没有选修过张学友老师的课程的所有学生的姓名
select s.SNAME from S WHERE NOT EXISTS(
    SELECT * FROM SC,C WHERE SC.CNO=C.CNO AND SC.SNO =S.SNO AND C.CTEACHER='张学友'
    );

-- 列出有两门以上(含两门)不及格课程(成绩小于60)的学生的姓名及其平均成绩
select s.SNAME,AVG(SC.SCGRADE) FROM S,SC WHERE S.SNO=SC.SNO AND sc.SCGRADE<60 GROUP BY S.SNAME HAVING COUNT(S.SNAME)>=2;

-- 列出既学过刘德华老师的课程,又学过张学友老师的课程的所有学生的姓名
select s.SNAME from s,(select sc.SNO from sc,c where sc.CNO=c.CNO and c.CTEACHER in('张学友','刘德华') GROUP BY sc.SNO HAVING COUNT(DISTINCT sc.CNO)=2) sc where s.SNO=sc.SNO;

 

posted @ 2017-08-24 10:22  Againn  阅读(272)  评论(0编辑  收藏  举报