MySQL案例:多表连接查询表格数据

多表连接查询

 

8030116a6b6260e0395f19d729ddc65f.png

解题思路: 

1.select course.CourseId,course.CourseName,teacher.* from course,teacher where course.TeaId=teacher.TeaId;
将课程表和教师表的信息合并,然后查询每个课程对应的老师

2.select course.CourseName,max(score.Score) from score,course where course.CourseId=score.CourseId group by score.CourseId;
将分数表和课程表合并,按照课程分组,再查询每科最大分数

3.select course.CourseName,t2.Score as '最高分',student.* from student,course,(select score.StuId,score.Score,score.CourseId from score,(select CourseId,max(Score) as m from score group by CourseId)t1 where score.Score=t1.m and score.CourseId = t1.CourseId)t2 where student.StuId= t2.StuId and t2.CourseId=course.CourseId order by t2.CourseId asc;
第一步是要先只知道每科最高分的分数值
第二步从分数表和分数值表中获取最高分学生的id、课程id、最高分值
第三步从上一个结果中和学生表、课程表中获取对应的学生信息,课程名

4.条件:总分最高
查询内容:学生信息
因为总分最高是多少不知道,所以先要查询,命名为t1:select sum(Score) as s1 from score group by StuId order by su desc limit 1;
统计总分按照学生分组t2:select StuId,sum(Score) as s2 from score group by StuId;
查询学生信息,从上面两张表和studen表:select student.*,t2.s2 from student,(select sum(Score) as s1 from score group by StuId order by s1 desc limit 1)t1,(select StuId,sum(Score) as s2 from score group by StuId)t2 where t1.s1 = t2.s2 and student.StuId=t2.StuId;

5.条件:花无缺,最高分
内容:性别
teacher-course-score-student
第一步先找花无缺带的班级:select TeaId from teacher where TeaName='花无缺';
第二步根据teaid从course表中找courseid:select course.CourseId from course,(select TeaId from teacher where TeaName='花无缺')t1 where course.TeaId = t1.TeaId;
第三步根据课程id去分数表里面找最高分的值:select t2.CourseId,max(score.Score) as m1 from score,(select course.CourseId from course,(select TeaId from teacher where TeaName='花无缺')t1 where course.TeaId = t1.TeaId)t2 where score.CourseId=t2.CourseId group by score.CourseId;
第四步才是统计各科最高分,找出花无缺那科id的最高分的所有人:select score.StuId,score.CourseId,score.Score from score,(select t2.CourseId,max(score.Score) as m1 from score,(select course.CourseId from course,(select TeaId from teacher where TeaName='花无缺')t1 where course.TeaId = t1.TeaId)t2 where score.CourseId=t2.CourseId group by score.CourseId)t3 where score.CourseId=t3.CourseId and score.Score = t3.m1 group by score.CourseId;
第五步根据上一个结果中的学生id去student表里面找学生性别:select student.StuSex from student,(select score.StuId,score.CourseId,score.Score from score,(select t2.CourseId,max(score.Score) as m1 from score,(select course.CourseId from course,(select TeaId from teacher where TeaName='花无缺')t1 where course.TeaId = t1.TeaId)t2 where score.CourseId=t2.CourseId group by score.CourseId)t3 where score.CourseId=t3.CourseId and score.Score = t3.m1 group by score.CourseId)t4 where t4.StuId=student.StuId

6.select student.StuName from student,(select score.StuId,score.CourseId,score.Score from score,(select t2.CourseId,min(score.Score) as m1 from score,(select course.CourseId from course,(select TeaId from teacher where TeaName='姚明')t1 where course.TeaId = t1.TeaId)t2 where score.CourseId=t2.CourseId group by score.CourseId)t3 where score.CourseId=t3.CourseId and score.Score = t3.m1 group by score.CourseId)t4 where t4.StuId=student.StuId

7.条件:每科最高分,人数>=2
内容:科目
第一步查每科最高分:select CourseId,max(Score) as m1 from score group by CourseId;
第二步从分数表中过滤科目和上一个一样,分数也一样的学生的信息:select score.*,t1.m1 from score,(select CourseId,max(Score) as m1 from score group by CourseId)t1 where score.CourseId=t1.CourseId and score.Score=t1.m1;
第三步统计大于2的courseid:select t2.CourseId,count(*) from (select score.*,t1.m1 from score,(select CourseId,max(Score) as m1 from score group by CourseId)t1 where score.CourseId=t1.CourseId and score.Score=t1.m1)t2 group by t2.CourseId having count(*) >=2;
第四步,那上面的结果去course表中找课程名字:select course.* from course,(select t2.CourseId,count(*) from (select score.*,t1.m1 from score,(select CourseId,max(Score) as m1 from score group by CourseId)t1 where score.CourseId=t1.CourseId and score.Score=t1.m1)t2 group by t2.CourseId having count(*) >=2)t3 where course.CourseId= t3.CourseId;

8.先查学生最多的三门课id:select CourseId,count(*) from score group by CourseId order by count(*) desc limit 3;
在根据id去课程表里找课程名字:select course.CourseId,course.CourseName from course,(select CourseId,count(*) from score group by CourseId order by count(*) desc limit 3)t1 where course.CourseId = t1.CourseId;

找均分最高的三门课id: select CourseId,avg(Score) from score group by CourseId order by avg(Score) desc limit 3;

再根据id去课程表中找名字:select course.CourseId,course.CourseName from course,(select CourseId,avg(Score) from score group by CourseId order by avg(Score) desc limit 3)t2 where course.CourseId = t2.CourseId;

最后一步,将两个表进行连接:(select course.CourseId as '前三个是人数最多的,后三个是均分最高的',course.CourseName from course,(select CourseId,count(*) from score group by CourseId order by count(*) desc limit 3)t1 where course.CourseId = t1.CourseId) union all (select course.CourseId,course.CourseName from course,(select CourseId,avg(Score) from score group by CourseId order by avg(Score) desc limit 3)t2 where course.CourseId = t2.CourseId)

9.先统计男生的总分:select * from student where StuSex = '男';
再拿id去统计总分:select t1.StuSex,sum(score.Score) from score,(select * from student where StuSex = '男')t1 where score.StuId=t1.StuId 

再统计女生的:select t2.StuSex,sum(score.Score) from score,(select * from student where StuSex = '女')t2 where score.StuId=t2.StuId 

最后联合两张表:(select t1.StuSex,sum(score.Score) from score,(select * from student where StuSex = '男')t1 where score.StuId=t1.StuId) union all (select t2.StuSex,sum(score.Score) from score,(select * from student where StuSex = '女')t2 where score.StuId=t2.StuId);

10.条件:英语、现行代数
内容:英语均分、线代总分
如果条件是已知的可以直接用,如果是未知的,则需要通过查询获取
第一步:select CourseId,CourseName from course where CourseName = '英语' or CourseName = '线性代数';
第二步,过滤出分数表里只属于英语和线代的数据:select score.CourseId,score.Score,t1.CourseName from score,(select CourseId,CourseName from course where CourseName = '英语' or CourseName = '线性代数')t1 where score.CourseId = t1.CourseId;
第三步,从上面的表中查英语均分:select avg(t2.Score) from (select score.CourseId,score.Score,t1.CourseName from score,(select CourseId,CourseName from course where CourseName = '英语' or CourseName = '线性代数')t1 where score.CourseId = t1.CourseId)t2 where t2.CourseName='英语';
第四步,从上上一个表中查线代总分:select sum(t3.Score) from (select score.CourseId,score.Score,t1.CourseName from score,(select CourseId,CourseName from course where CourseName = '英语' or CourseName = '线性代数')t1 where score.CourseId = t1.CourseId)t3 where t3.CourseName='线性代数';
最后一步合并:(select t2.CourseName,avg(t2.Score) as '结果' from (select score.CourseId,score.Score,t1.CourseName from score,(select CourseId,CourseName from course where CourseName = '英语' or CourseName = '线性代数')t1 where score.CourseId = t1.CourseId)t2 where t2.CourseName='英语') union all (select t3.CourseName,sum(t3.Score) from (select score.CourseId,score.Score,t1.CourseName from score,(select CourseId,CourseName from course where CourseName = '英语' or CourseName = '线性代数')t1 where score.CourseId = t1.CourseId)t3 where t3.CourseName='线性代数');

11.条件:单科分数>单科均分
内容:学生信息
第一步先查单科均分:select CourseId,avg(Score) as a1 from score group by CourseId;
第二步找单科分数>单科均分的: select score.*,t1.a1 from score,(select CourseId,avg(Score) as a1 from score group by CourseId)t1 where score.CourseId=t1.CourseId and score.Score > t1.a1;
第三步拿上面的学生id去学生表里找信息:select student.*,t2.CourseId,t2.Score,t2.a1 from student,(select score.*,t1.a1 from score,(select CourseId,avg(Score) as a1 from score group by CourseId)t1 where score.CourseId=t1.CourseId and score.Score > t1.a1)t2 where student.StuId = t2.StuId;

上面是不去重的写法,现在展示去重的写法:select distinct(t3.StuId) from (select student.* from student,(select score.*,t1.a1 from score,(select CourseId,avg(Score) as a1 from score group by CourseId)t1 where score.CourseId=t1.CourseId and score.Score > t1.a1)t2 where student.StuId = t2.StuId)t3;

12.select t3.StuSex,count(*) from (select student.*,t2.CourseId,t2.Score,t2.a1 from student,(select score.*,t1.a1 from score,(select CourseId,avg(Score) as a1 from score group by CourseId)t1 where score.CourseId=t1.CourseId and score.Score > t1.a1)t2 where student.StuId = t2.StuId)t3 group by t3.StuSex;

去重的:select student.StuSex,count(*) from student,(select distinct(t3.StuId) from (select student.* from student,(select score.*,t1.a1 from score,(select CourseId,avg(Score) as a1 from score group by CourseId)t1 where score.CourseId=t1.CourseId and score.Score > t1.a1)t2 where student.StuId = t2.StuId)t3)t4 where student.StuId=t4.StuId group by student.StuSex;

13.先找有一个及格的人的id,去重:select distinct(StuId) from score where Score >= 60;
统计有成绩的人的id:select distinct(score.StuId) as d2 from score;

select StuSex,count(*) from student, (select distinct(StuId) from score where StuId not in (select distinct(StuId) from score where Score>=60))t1 where student.StuId=t1.StuId group by StuSex; 

14.select StuSex,count(*) from student,(select distinct(StuId) from score where StuId not in (select distinct(StuId) from score where Score <60))t1 where student.StuId=t1.StuId and student.StuAge >=18 group by StuSex;
先找只要有一科不及格的人的id,然后从有成绩的人里面过滤掉这些id,剩下的就是每科都及格的人的id,去重,然后拿这个id去学生表里按照性别分组,统计年龄大于等于18的人数即可。

 

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Javaweb是一种使用Java语言编写的Web开发技术,结合JSP(JavaServer Pages)和Servlet(Java Servlet)可以实现动态Web页面的开发。MySQL是一种开源的关系型数据库管理系统,可以使用SQL语言对其进行操作。 一个典型的Javaweb JSP Servlet MySQL案例源码可以如下: 1. 首先,我们可以创建一个简单的数据库表格,例如一个学生信息表格,包含学生ID、姓名和年龄等字段。 2. 创建一个数据库连接类,用于连接MySQL数据库。在这个类中,我们需要配置数据库连接参数,如数据库URL、用户名和密码等。 3. 创建一个Servlet类,用于处理前端页面请求。在这个类中,我们可以编写处理逻辑,例如查询学生信息、插入新的学生记录等操作。可以使用JDBC(Java Database Connectivity)来实现数据库的增删改查操作。 4. 创建一个JSP页面,用于展示数据。在这个页面中,可以使用JSP的标签和表达式语言来获取Servlet返回的数据,并在页面中进行展示。 5. 在web.xml文件中配置Servlet和JSP的映射关系,以及其他必要的配置。 通过以上步骤,我们可以实现一个简单的Javaweb JSP Servlet MySQL案例。用户可以通过前端页面输入查询条件,后端Servlet会将查询结果从数据库中获取并返回给JSP页面进行展示。同时,用户还可以通过前端页面提交数据,后端Servlet会将数据插入到数据库中。 这个案例可以用于教学或者实际项目开发中,通过理解和学习这个案例,可以了解Javaweb开发的基本流程,以及如何使用JSP、Servlet和MySQL进行Web开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值