mysql多表操作例题_MySQL多表查询练习题

本文提供了一系列MySQL多表查询的实例,包括查询未学全所有课程的学生、查找课程完全相同的同学、删除特定教师授课记录、插入新记录、显示学生三门课程成绩、各科成绩高低及及格率、成绩前三名、课程选修人数、同名同姓学生、平均成绩筛选、特定课程低分学生、高分学生、选修指定课程学生、选修人数、最佳成绩学生、课程选修人数、成绩相同学生、最佳成绩前两名、选修两门以上课程的学生、全班选修课程、未学特定教师课程的学生、多门不及格学生、课程低分学生记录删除等。通过这些实例,深入理解并掌握MySQL的多表操作技巧。
摘要由CSDN通过智能技术生成

1、查询没有学全所有课的同学的学号、姓名;#先统计一共有多少门课程

select count(cid)fromcourse;#查看每个学生选择的课程书

select count(course_id)fromscore group by student_id;#查询所学课程数小于总课程数的学生学号

select student_idfrom (select count(course_id) c_course_id,student_id fromscore group by student_id) t1

where t1.c_course_id< (select count(cid) fromcourse) ;#查询没有学全所有课的同学的学号、姓名;

select sid,snamefrom student where sid in(

select student_idfrom (select count(course_id) c_course_id,student_id fromscore group by student_id

) t1 where t1.c_course_id< (select count(cid) fromcourse)

) ;2、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;#先查询2号同学学了哪些课程

select* from score where student_id =2;#找到学习了2号同学没学习课程的所有同学(找到所有和2号同学学习的课程不一样的同学)

select student_idfrom score where course_id not in (select course_id from score where student_id=2)#找到score表中所有的学生并且把 2号同学 以及(和2号同学学习的课程不一样的同学)排除出去

select student_idfrom score where student_id not in (select student_id from score where course_id not in (select course_id from score where student_id=2)) and student_id !=2

#对剩余的和2号同学所选课程没有不同的同学所选课程数进行统计,如果和2号同学的课程数相同,就是选择了相同的课程

select student_idfrom score where student_id not in(

select student_idfrom score where course_id not in (select course_id from score where student_id=2)

)and student_id !=2group by student_id

having count(course_id)= (select count(course_id) from score where student_id=2);3、删除学习“叶平”老师课的SC(score)表记录;#先查出李平老师的id

select tidfrom teacher where tname = '李平老师';#查看李平老师所教授的课程

select cidfrom course where teacher_id = (select tid from teacher where tname = '李平老师’);

#查看李平老师所教课程的成绩数据

select* from score where course_id in (select cid from course where teacher_id = (select tid from teacher where tname = '李平老师'));#执行删除命令

deletefrom score where course_id in (select cid from course where teacher_id = (select tid from teacher where tname = '李平老师'));4、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;#先找寻上过2号课程的同学

select student_idfrom score where course_id = 2;#再找到没上过2号课程的所有同学

select* from student where sid not in (select student_id from score where course_id = 2);#计算出学习2号课程的同学的平均成绩

select avg(num)from score where course_id = 2group by course_id;#用笛卡尔积将上述两个表拼起来

select* f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值