有这么一些字段和表
像我这里做法就是懒人做法,我觉得多连几张表无所谓,就不用去想是连接哪几张表了
1.根据学生ID查询 (授课老师 授课时间 课程名称 结束时间 班级名称 周几 ) 按时间排序//老师表,安排表,班级表,课程表,学生表
select tname,start_time,cname,end_time,bname,DAYOFWEEK(start_time)-1 as Date from tb_arrange as a LEFT JOIN tb_ban as b on a.bid=b.id join tb_student as c on b.id=c.tid JOIN tb_teacher as d on a.tid=d.tid join tb_course as e on e.cid=a.kid WHERE c.id=1 ORDER BY start_time asc
2.查询所有班级学生总分 高低排列 (班级id 班级名称 班级总分 班级单科成绩最好(语文 OR 数学 or 英文) 平均分最高姓名)
SELECT cc.id,cc.bname,SUM(achievement ) as sum,
(select SUM(achievement) as sut from tb_achievement as a left join tb_student as b on a.sid=b.id join tb_ban as c on b.tid=c.id JOIN tb_course as d on a.kname=d.cname where b.tid=cc.id GROUP BY c.id,d.cname ORDER BY sut desc limit 1) as suti,(select d.cname from tb_achievement as a left join tb_student as b on a.sid=b.id join tb_ban as c on b.tid=c.id JOIN tb_course as d on a.kname=d.cname where b.tid=cc.id GROUP BY c.id,d.cname ORDER BY SUM(achievement) desc limit 1) as skname,
(select b.name as abf from tb_achievement as a left join tb_student as b on a.sid=b.id join tb_ban as c on b.tid=c.id JOIN tb_course as d on a.kname=d.cname where c.id=cc.id GROUP BY b.id ORDER BY avg(achievement) desc limit 1) as takname FROM tb_achievement as aa left join tb_student as bb on aa.sid=bb.id join tb_ban as cc on bb.tid=cc.id GROUP BY cc.id
3.查询课程不及格的学生 打印出所有不及格的成绩 按班级 按分数排列 (学生名称 学生分数 课程名称 班级名称 所有不及格学生的平均分)成绩表,学生表,班级表,课程表
select d.`name`,achievement,c.cname,bb.bname,(select AVG(achievement) FROM tb_arrange as a LEFT JOIN tb_ban as b on a.bid=b.id JOIN tb_course as c on a.kid=c.cid JOIN tb_student as d on b.id=d.tid join tb_achievement as e on d.id=e.sid WHERE achievement<60 and b.id=bb.id GROUP BY bb.id) as avg FROM tb_arrange as a LEFT JOIN tb_ban as bb on a.bid=bb.id JOIN tb_course as c on a.kid=c.cid JOIN tb_student as d on bb.id=d.tid join tb_achievement as e on d.id=e.sid GROUP BY d.id
4.根据周几和班级id查询班级当天的课程表(班级名称,任课老师,上的什么课,课程开始时间,课程结束时间)班级表,老师表,安排表,课程表
select start_time,d.tname,end_time,bname,c.cname from tb_arrange as a LEFT JOIN tb_ban as b on a.bid=b.id join tb_course as c on a.kid=c.cid JOIN tb_teacher as d on a.tid=d.tid WHERE DAYOFWEEK(start_time)-1=1 and b.id=1
5.查询每个班级每门课程的最高分和最低分(班级名称,课程名,最高分数,最低分数)班级表,课程表,成绩表 学生表
select bname,e.cname,MAX(achievement) as max ,MIN(achievement) as min from tb_arrange as a LEFT JOIN tb_ban as b on a.bid=b.id join tb_student as c on b.id=c.tid JOIN tb_teacher as d on a.tid=d.tid join tb_course as e on e.cid=a.kid JOIN tb_achievement as f on c.id=f.sid GROUP BY b.id,kname