mysql学习笔记3.5
紧接着笔记3的训练
介绍一个对于我来说的比较难的难点!!!!
select * from sc;
select student.sno,sname,avg(grade) as 平均分 from student,sc where student.sno=sc.sno group by student.sno having avg(grade)>90/*这一个条件我竟然忘记写了qwq*/ order by grade desc;
我刚才写的时候忘记了 平均分高于90分这个条件了!!!!!!!!!!!
分组之后还有一个having来筛选之!!!!!!
having子句与where有相似之处但也有区别,都是设定条件的语句。
having 是筛选组 而where是筛选记录。
go on----------->
select student.sno,sname,avg(grade) as 平均分 from student,sc where student.sno=sc.sno group by student.sno having avg(grade)>90/*这一个条件我竟然忘记写了qwq*/ order by grade desc limit 2;
2.接下来开始自连接,外连接,全连接之类啦,学习领会掌握一边SQL语言化的关系代数语言哦,2333!
自连接:
select * from student;
/*查询和龙龙在同一学院学习的同学的姓名*/ select s2.sname from student as s1,student as s2 where s1.sname='龙龙' and s1.sdeptno=s2.sdeptno;
查询每一门课的间接先修课
select c1.cno,c1.cname,c2.cpre as cppre from c as c1,c as c2 where c1.cpre=c2.cno;/*求每一门课的间接选修课*/
说上一句相关的话哒哒哦:
可以这样给表设置外码
课程表中的先修课就是课程表的外码!!!!!!!
https://www.cnblogs.com/xiohao/archive/2013/06/28/3160265.html
alter table c add constraint FK_c foreign key(cpre) references c(cno);/*将课程表c的cpre字段设置为c的外码,参照关系也是c*/
下一个问题--------》
代码和结果如下下:
select c1.cno,c1.cname,c2.cpre as cppre from c as c1,c as c2 where c1.cpre=c2.cno;/*求每一门课的间接选修课*/
/*左外连接*/ /*查询所有学生的...情况*/ select sname,cno,grade from student left join sc on student.sno=sc.sno;
右外连接
/*查询所有的课程信息,...*/ select cname,sc.sno,grade from sc right outer join c on sc.cno=c.cno;/*右连接*/