mysql多表查询前15题https://www.cnblogs.com/Eva-J/articles/9688383.html

时间:2020-04-06
本文章向大家介绍mysql多表查询前15题https://www.cnblogs.com/Eva-J/articles/9688383.html,主要包括mysql多表查询前15题https://www.cnblogs.com/Eva-J/articles/9688383.html使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
# 1、查询男生、女生的人数;
# select gender,count(*) from student group by gender;
# 2、查询姓“张”的学生名单;
# select * from student where sname like '张%';
# 3、课程平均分从高到低显示
# select course_id,avg(num) as a from score group by course_id order by a desc; #查出了各个部门的平均分
# 4、查询有课程成绩小于60分的同学的学号、姓名;
# select sid,sname from student where sid in (select distinct student_id from score where num<60);
# 5、查询至少有一门课与学号为1的同学所学课程相同的同学的学号和姓名;
# 学号1学的课程 select course_id from score where student_id =1;
# 相同课程的学号 select distinct student_id from score where course_id in (select course_id from score where student_id =1);
# select sid,sname from student where sid in(select distinct student_id from score where course_id in (select course_id from score where student_id =1) and student_id !=1);
# 6、查询出只选修了一门课程的全部学生的学号和姓名;
# #学生们的选课情况 select student_id from score group by student_id having count(sid)=1;
# # having select sname,sid from student where sid =(select student_id from score group by student_id having count(sid)=1);
# 7、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
# select course_id,max(num),min(num) from score group by course_id;
# 8、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
# 把课程1,2的所有人的学生id,成绩找出来
# select student_id as sid1,num as n1 from score where course_id =1 ;
# select student_id as sid2,num as n2 from score where course_id =2 ;
# 取课程2成绩比课程1低的所有同学的学好
# select sid1 from( select student_id as sid1,num as n1 from score where course_id =1)as t1
# inner join
# (select student_id as sid2,num as n2 from score where course_id =2) as t2
# on t1.sid1=t2.sid2 where n2<n1;
# 连表student表取名字和学号
# select sid,sname from student right join(
# select sid1 from( select student_id as sid1,num as n1 from score where course_id =1)as t1
# inner join
# (select student_id as sid2,num as n2 from score where course_id =2) as t2
# on t1.sid1=t2.sid2 where n2<n1
# )as tmp
# on student.sid = tmp.sid1;
# 9、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
# 现获取生生物课,物理课的id
# select cid from course where cname='生物';
# select cid from course where cname='物理';
# 找到所有血生物,物理的人的学号和分数
# select student_id as sid1,num as n1 from score where course_id=(select cid from course where cname='生物')
# select student_id as sid2,num as n2 from score where course_id=(select cid from course where cname='物理')
# select sid from student right join(
# select sid1 from (select student_id as sid1,num as n1 from score where course_id=(select cid from course where cname='生物')) as t1
# inner join
# (select student_id as sid2,num as n2 from score where course_id=(select cid from course where cname='物理'))as t2
# on t1.sid1=t2.sid2 where n1 >n2
# )as tmp
# on student.sid=tmp.sid1
# 10、查询平均成绩大于60分的同学的学号和平均成绩;
# select sid,avg(num) from score group by student_id having avg(num)>60;
# 11、查询所有同学的学号、姓名、选课数、总成绩;
# select student_id,count(course_id)as '选课数',sum(num)as '总成绩' from score group by student_id
# select student_id,sname,c,b from student as t1
# inner join
# (select student_id,count(course_id)as c,sum(num)as b from score group by student_id)as t2
# on t1.sid=t2.student_id
# 12、查询姓“李”的老师的个数;
# select count(tid) from teacher where tname like '李%';
# 13、查询没学过“张磊老师”课的同学的学号、姓名;
# 张磊老师教的课程 select cid from course where teacher_id=(select tid from teacher where tname ='张磊老师');
# 学过张磊老师课的同学的student_id select student_id from score where course_id=( select cid from course where teacher_id=(select tid from teacher where tname ='张磊老师'));
# 没学过的同学id select sid,sname from student where sid not in (select student_id from score where course_id=( select cid from course where teacher_id=(select tid from teacher where tname ='张磊老师')));
# 14、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;
# 学过1的同学 select student_id from score where course_id =1;
# 是否学过2的同学学号 select student_id from score where student_id in (select student_id from score where course_id =1) and course_id=2;
# 姓名 select sid,sname from student where sid in(select student_id from score where student_id in (select student_id from score where course_id =1) and course_id=2);
# 15、查询学过“李平老师”所教的所有课的同学的学号、姓名;
# 李平老师教的课 select cid from course where teacher_id=(select tid from teacher where tname='李平老师');
# 学过李平老师课的学生 select distinct student_id from score where course_id in (select cid from course where teacher_id=(select tid from teacher where tname='李平老师'));
# 学生的学号和姓名 select sid,sname from student where sid in(select distinct student_id from score where course_id in (select cid from course where teacher_id=(select tid from teacher where tname='李平老师')));

原文地址:https://www.cnblogs.com/zhaolei1986/p/12640660.html