學習地址:一天學會 MySQL 數據庫【https://www.bilibili.com/video/BV1Vt411z7wy】
目 錄
12、查詢score表中至少有2名學生選修的並以3開頭的課程的平均分數。
16、查詢所有學生的sname、cname和degree列。
18、查詢選修“3-105"課程的成績高於“109”號同學“3-105"成績的所有同學的記錄。
22-查詢練習-分組計算平均成績
11、查詢每門課的平均成績
select * from course;
select avg(degree) from score where cno= '3-105';
select avg(degree) from score where cno= '3-245';
select avg(degree) from score where cno= '6-166';
select avg(degree) from score where cno= '9-888';
select degree from score where cno= '3-105';
select degree from score where cno= '9-888';
select avg(degree) from score group by cno;
select cno, avg(degree) from score group by cno;
23-查詢練習-分組條件與模糊查詢
12、查詢score表中至少有2名學生選修的並以3開頭的課程的平均分數。
having 和 group by 配合使用,目的是過濾分組中的數據。
having子句,在聚合後 對組記錄進行篩選。
select cno from score group by cno;
select cno from score group by cno having count(cno) >= 2;
select cno from score group by cno having count(cno) >= 4;
select cno from score group by cno having count(cno) >= 2 and cno like '3%';
select cno, avg(degree) from score group by cno having count(cno) >= 2 and cno like '3%';
select cno, avg(degree), count(*) from score group by cno having count(cno) >= 2 and cno like '3%';
24-查詢練習-範圍查詢的兩種方式
13、查詢分數大於70,小於90的sno列。
select sno, degree from score where degree > 70 and degree < 90; /*開區間:(70, 90) */
select sno, degree from score where degree between 70 and 90; /*閉區間:[70, 90] */
25-查詢練習-多表查詢
14、查詢所有學生的sname、cno和degree列。
select sname from student;
select cno, degree from score;
/*----------------加上sno----------------*/
select sno, sname from student;
select sno, cno, degree from score;
/*----------------多表連接查詢----------------*/
select sname, cno, degree from student, score
where student.sno = score.sno;
26-查詢練習-多表查詢
15、查詢所有學生的cname、sno和degree列。
select cno, cname from course;
select cno, sno, degree from score;
select cname, sno, degree from course, score
where course.cno = score.cno;
27-查詢練習-三表關聯查詢
16、查詢所有學生的sname、cname和degree列。
sname -> student
cname -> course
degree -> score共同字段
select sname, cname, degree from student, course, score
where student.sno = score.sno
and course.cno = score.cno;
select sname, cname, degree, student.sno, course.cno from student, course, score
where student.sno = score.sno
and course.cno = score.cno;
select sname, cname, degree, student.sno as stu_sno,
course.cno as cou_cno from student, course, score
where student.sno = score.sno
and course.cno = score.cno;
select sname, cname, degree, student.sno as stu_sno, score.sno, score.cno
course.cno as cou_cno from student, course, score
where student.sno = score.sno
and course.cno = score.cno;
28-查詢練習-子查詢 加 分組求平均分
17、查詢"95031"班學生每課的平均分。
select * from student where class = '95031';
select sno from student where class = '95031';
select * from score where sno in ( select sno from student where class = '95031' );
select cno, avg(degree) from score
where sno in (select sno from student where class='95031' )
group by cno;
29-查詢練習-子查詢
18、查詢選修“3-105"課程的成績高於“109”號同學“3-105"成績的所有同學的記錄。
題意是:“3-105”這門課程裏面,109號同學成績爲x,成績>x的其他同學的記錄。
select degree from Score where sno = '109' and cno = '3-105' ;
select * from Score where degree > ( select degree from Score where sno = '109' and cno = '3-105' );
select * from Score where degree > ( select degree from Score where sno = '109' and cno = '3-105' ) and cno = '3-105';