标签:course from ber max HERE end teacher work 学生
-- 2.查询每个老师教授的课程数量 和 老师信息
mysql> select teacher.tid,teacher.tname,count(course.teacher_id)
-> from course left join teacher on course.teacher_id=teacher.tid
-> group by course.teacher_id ;
+------+--------+--------------------------+
| tid | tname | count(course.teacher_id) |
+------+--------+--------------------------+
| 1 | 波多 | 2 |
| 2 | 苍空 | 1 |
+------+--------+--------------------------+
2 rows in set (0.00 sec)
-- 3. 查询学生的信息以及学生所在的班级信息
mysql> select student.*,class.caption from student left join class on student.class_id=class.cid;
+-----+--------+--------+----------+---------------+
| sid | sname | gender | class_id | caption |
+-----+--------+--------+----------+---------------+
| 1 | 钢蛋 | female | 1 | class2 grade3 |
| 2 | 铁锤 | female | 1 | class2 grade3 |
| 3 | 山炮 | male | 2 | class3 grade1 |
+-----+--------+--------+----------+---------------+
3 rows in set (0.00 sec)
-- 4、学生中男生的个数和女生的个数
mysql> select count(student.gender=‘male‘),count(student.gender=‘female‘)from student;
+------------------------------+--------------------------------+
| count(student.gender=‘male‘) | count(student.gender=‘female‘) |
+------------------------------+--------------------------------+
| 3 | 3 |
+------------------------------+--------------------------------+
1 row in set (0.00 sec)
-- 5、获取所有学习‘生物‘的学生的学号和成绩;姓名
mysql> select student.sid,score.number,student.sname from course
-> left join score on course.cid=score.course_id
-> left join student on student.sid = score.student_id
-> where course.cid=1;
+------+--------+--------+
| sid | number | sname |
+------+--------+--------+
| 1 | 60 | 钢蛋 |
+------+--------+--------+
1 row in set (0.00 sec)
-- 6、查询平均成绩大于60分的同学的学号和平均成绩;
mysql> select student.sid ,avg(score.number)
-> from score left join student on student.sid = score.student_id
-> group by score.student_id;
+------+-------------------+
| sid | avg(score.number) |
+------+-------------------+
| 1 | 59.5000 |
| 2 | 100.0000 |
+------+-------------------+
2 rows in set (0.39 sec)
-- 7、查询姓“李”的老师的个数;
mysql> select count(teacher.tid) from teacher where teacher.tname = ‘李%‘;
+--------------------+
| count(teacher.tid) |
+--------------------+
| 0 |
+--------------------+
1 row in set (0.00 sec)
-- 8、查询课程成绩小于60分的同学的学号、姓名;
mysql> select student.sid,student.sname
-> from student left join score on student.sid = score.student_id
-> where score.number < 60;
+-----+--------+
| sid | sname |
+-----+--------+
| 1 | 钢蛋 |
+-----+--------+
1 row in set (0.00 sec)
-- 9. 删除学习“叶平”老师课的SC表记录
mysql> delete from course
-> left join teacher on teacher.tid=course.teacher_id
-> left join score on score.course_id = course.cid
-> where teacher.tname = ‘叶萍‘;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘left join teacher on teacher.tid=course.teacher_id
left join score on score.cour‘ at line 2
mysql> delete teacher.tname from course
-> left join teacher on teacher.tid=course.teacher_id
-> left join score on score.course_id = course.cid
-> where teacher.tname = ‘叶萍‘;
ERROR 1109 (42S02): Unknown table ‘tname‘ in MULTI DELETE
-- 10.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
mysql> select course.cid,max(score.number),min(score.number)
-> from course left join score on course.cid=score.course_id;
+-----+-------------------+-------------------+
| cid | max(score.number) | min(score.number) |
+-----+-------------------+-------------------+
| 1 | 100 | 59 |
+-----+-------------------+-------------------+
1 row in set (0.40 sec)
-- 11.查询每门课程被选修的学生数
mysql> select course.cname ,count(score.course_id) from course
-> left join score on course.cid = score.course_id
-> left join student on student.sid = score.student_id
-> group by score.course_id;
+--------+------------------------+
| cname | count(score.course_id) |
+--------+------------------------+
| 物理 | 0 |
| 生物 | 1 |
| 体育 | 2 |
+--------+------------------------+
3 rows in set (0.00 sec)
-- 12.查询姓“张”的学生名单;
mysql> select student.sname from student where student.sname = ‘张%‘;
Empty set (0.00 sec)
-- 13.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
mysql> select avg(score.number) from score group by course_id order by avg(number) asc ,score.course_id desc;
+-------------------+
| avg(score.number) |
+-------------------+
| 60.0000 |
| 79.5000 |
+-------------------+
2 rows in set (0.00 sec)
-- 14.查询平均成绩大于85的所有学生的学号、姓名和平均成绩
mysql> select student.sid,student.sname,avg(score.number)
-> from score left join student on score.student_id = student.sid
-> group by score.student_id
-> having avg(score.number) > 85;
+------+--------+-------------------+
| sid | sname | avg(score.number) |
+------+--------+-------------------+
| 2 | 铁锤 | 100.0000 |
+------+--------+-------------------+
1 row in set (0.00 sec)
-- 15.查询课程编号为3且课程成绩在80分以上的学生的学号和姓名;
mysql> select student.sid,student.sname from student
left join score on score.student_id=student.sid
left join course on course.cid = score.course_id
where course.cid=3 and score.number>80;
Empty set (0.00 sec)
-- 16.查询各个课程及相应的选修人数
mysql> select course.cname,count(score.course_id)
from course left join score on score.course_id=course.cid
group by score.course_id;
+--------+------------------------+
| cname | count(score.course_id) |
+--------+------------------------+
| 物理 | 0 |
| 生物 | 1 |
| 体育 | 2 |
+--------+------------------------+
3 rows in set (0.00 sec)
-- 17.查询“2”课程分数小于60,按分数降序排列的同学学号
mysql> select student.sid from student
left join score on score.student_id=student.sid
left join course on course.cid = score.course_id
where course.cid = 2 and score.number < 60;
+-----+
| sid |
+-----+
| 1 |
+-----+
1 row in set (0.00 sec)
-- 18.删除学号为“2”的同学的“1”课程的成绩
mysql> delete score.number from student
left join score on score.student_id = student.sid
left join course on course.cid = score.course_id
where student.sid = 2 and course.cid = 1 ;
ERROR 1109 (42S02): Unknown table ‘number‘ in MULTI DELETE
标签:course from ber max HERE end teacher work 学生
原文地址:https://www.cnblogs.com/agsol/p/11768246.html