1.整体表字段如下图所示:
2.每个表的详情:
class班级表:
course课程表:
score成绩表:
student学生表:
teacher教师表:
3.相关题目以及答案
-- 1.查询所有的课程的名称以及对应的任课老师姓名?
-- SELECT co.name course_name,te.name teacher_name FROM course co LEFT JOIN teacher te ON co.teacher_id = te.id;
-- 2.查询平均成绩大于65分的同学的id、姓名和平均成绩
-- SELECT st.id,st.name,avg(sc.mark) svg FROM student st LEFT JOIN score sc ON st.id = sc.student_id GROUP BY st.id HAVING svg>65;
-- 3.查询所有同学的姓名,选课数,总成绩
-- SELECT st.name,count(sc.course_id) course_count,sum(sc.mark) FROM student st LEFT JOIN score sc ON st.id = sc.student_id GROUP BY st.id;
-- 4.查询挂科超过两门(包括两门)的学生姓名
-- SELECT st.name FROM student st LEFT JOIN score sc ON st.id = sc.student_id WHERE sc.mark<60 GROUP BY st.id HAVING count(sc.mark)>=2;
-- 5.查询每门课程名字及其被选修次数
-- SELECT co.name,count(sc.course_id) count_course FROM score sc INNER JOIN course co ON sc.course_id = co.id GROUP BY sc.course_id;
-- 6.查询表中男生、女生各有多少人?
-- SELECT gender,count(st.gender) gender_count FROM student st GROUP BY st.gender;
-- 7.查询每门课程的平均成绩,结果按平均成绩升序排列
-- SELECT co.name,avg(sc.mark) svg FROM score sc LEFT JOIN course co ON sc.course_id = co.id GROUP BY sc.course_id ORDER BY svg;
-- 8.查询高三二班的学生
-- SELECT c.name class_name,st.name student_name FROM student st INNER JOIN class c ON st.class_id = c.id AND c.name = '高三2班';
4.写在最后
mysql的学习需要多加练习,以上答案不唯一,可能有错误或不足之处,还在学习中,请见谅。