mysql查询未讲课教师_经典mysql查询语言练习之拆分组合

本文通过一系列的MySQL查询语句示例,详细解析如何查询未讲课的教师,以及如何进行多表联查和复杂条件筛选。通过对实际场景的拆解,演示了如何组合使用子查询和联合查询来实现复杂的数据分析。
摘要由CSDN通过智能技术生成

上次去面试的时候,做笔试题发现mysql查询语句的题目写得很乱,涂涂改改的!!从现在开始,每天三条mysql查询语句,并且写下逻辑关系,找出逻辑规律进行拆分组合!!!!这素自己总结的方法哦

-----------------------------

表格如下:

student表

f14cd0e6b32778b87959f618e25abbd6.png

score表

16f09c17e97454a8e27c72a727e0037f.png

course表

67a2ee1253f320ae50f701033ec3be94.png

teacher表

7aefd005b59d6aa982274b974ea231cd.png

#查询由“张旭“教师任课的课程的学生成绩。

拆解句子:主谓宾是(查询学生成绩)→select degree from score

这句话需要用到的表有,score(sno,cno,degree)、course(cno,cname,tno)、teacher(tno,tname)

流程:首先在teacher表中查张旭老师的tno,再根据tno去course里查cno,再根据cno去(查score里的degree)

句子:

①select tno from teacher where tname="张旭"

②select cno from course

③select degree from score

组合如下:(顺序是③②①)

select degree from score where cno in(select cno from course where tno in(select tno from teacher where tname="张旭"));

#查询选修某课程的同学人数多于5人的教师姓名

拆解句子:主谓宾是(查询教师姓名)→select tname from teacher

这句话要用的表有score、course、teach

查询流程:在score表里计算选修cno的同学大于5的人数,找出了这些cno,再根据对应的cno去course里找到对应的tno,再根据tno(在teacher表里找tname)

①select cno from score group by cno having count(*)>5

②select tno from course

③selct tname from teacher

组合:select tname from teacher where tno in(select tno from course where cno in(select cno from score group by cno having count(*)>5));(顺序是③②①)

select tname from teacher where tno=(select tno from course where cno=(select cno from score group by cno having count(*)>5));

#查询出“计算机系“教师所教课程的成绩表

拆解句子:主谓宾(查询成绩)→select degree from score

查询流程:先在teacher表里找出计算机系老师对应的tno,再根据tno在course表里找到cno,再根据cno在score表里找degree

①select tno from teacher where depart="计算机系"

②select cno from course

③select degree from score

组合句子:select degree from score where cno in(select cno from course where tno in(select tno from teacher where depart="计算机系")

select degree from score where cno in(select cno from course where tno in(select tno from teacher where depart="计算机系")

#查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

拆解句子:主谓宾(查询cno,sno,degree)→select cno,sno,degree from student,score

查询流程:在score、student里查选修编号为“3-105”的同学的cno,sno,degree,把“3-105”同学的成绩大于所需要的成绩,要降序

①select cno,sno,degree from score,student where cno=“3-105”

②degree > any(select degree from score where cno="3-245")

③order by degree desc

组合:select cno,sno,degree from score where cno=“3-105” and degree > any(select degree from score where cno="3-245" order by degree desc;

select cno,sno,degree from score where cno=“3-105” and degree > any(select degree from score where cno="3-245" order by degree desc

#查询所有教师和同学的name、sex和birthday

分析:逻辑比较简单,主要用到一个功能:union

句子:select sname,sex,sbirthday from student union select tname,tsex,tbirthday from teacher;

select tname,tsex,tbirthday from Teacher union select sname,ssex,sbirthday from Student;

#查询所有“女”教师和“女”同学的name、sex和birthday.

select sname,ssex,sbirthday from student where ssex=“女” union select tname,tsex,tbirthday from teacher where tsex=“女”;

select sname,ssex,sbirthday from student where ssex="女" union select tname,tsex,tbirthday from teacher where tsex="女";

#查询成绩比该课程平均成绩低的同学的成绩表

拆解句子:主谓宾(查询成绩)→select degree from score

查询流程:先在score表里把sno一样的平均分算出来,再将score表里比cno平均分低的sno找出来,再在score表里把这些sno的degree找出来

②select avg(degree)from score

③select sno from score where degree < ..

④select degree from score

组合:select degree from score where sno in(select sno from score where degree

select degree from score where sno in(select sno from score where degree

#查询所有未讲课的教师的Tname和Depart.

拆解句子:主谓宾(查询tname,depart)→select tname,depart from teacher

查询流程:先在course里看有哪些tno,再在score表里查看tno,不存在于score表的tno,在teacher表里查看这种tno的tname,depart

①select tno from course

②select tno from score

③tno from course where tno not in(select tno from score)

④select tname,depart from teacher

组合句子:select tname,depart from teacher where tno=(select tno from score));

select tname,depart from teacher where tno=(select tno from score));

#查询至少有2名男生的班号。

主谓宾(查询班号)→select class from student

流程:select class from student where ssex='男' group by class having count(*)>=1;

#查询Student表中不姓“王”的同学记录。

主谓宾(查询同学记录)→select * from student

逻辑:在student表中找出姓不是王的同学

句子:select * from student where sname not like “王%”

select * from student where sname not like “王%”

#询Student表中每个学生的姓名和年龄

select sname,year(now())-year(sbirthday) from student;

select sname,year(now())-year(sbirthday) from student;

#查询Student表中最大和最小的Sbirthday日期值。

select max(sbirthday),min(sbirthday) from student;

#查询“男”教师及其所上的课程 不太会用这种”=“的,需要好好了解下

主谓宾(查询教师姓名及课程)→select tname,cname from course

流程:先在teacher表里查询sex为男的tname和tno,再根据tno在course表里查找对应的cname

逻辑:select sno,cno,degree from score where degree=(select max(degree) from score)

select sno,cno,degree from score where degree=(select max(degree) from score)

排序写法:

select sno,cno,degree from score order by degree desc limit 0,1;

select sno,cno,degree from score order by degree desc limit 0,1;

#查询和“李军”同性别的所有同学的Sname.

select sname from student where ssex=(select ssex from student where sname="李军");

select sname from student where ssex=(select ssex from student where sname="李军");

#查询和“李军”同性别并同班的同学Sname.

select sname from student where ssex=(ssex from student where sname="李军") and class=(select class from student where sname="李军");

select sname from student where ssex=(ssex from student where sname="李军") and class=(select class from student where sname="李军");

#查询所有选修“计算机导论”课程的“男”同学的成绩表。

主谓宾(查询成绩表)→select * from score

逻辑流程:在student表里找ssex为男的sno,在course表里找cname为计算机导论对应的cno,在score表里找sno和cno都符合的degree

①select sno from student where ssex=“男”

②select cno from course where cname=“计算机导论”

③select degree from score where ...

组合:select * from score where cno=(select cno from course where cname="计算机导论") and sno in(select sno from student where ssex="男");

select * from score where cno=(select cno from course where cname="计算机导论") and sno in(select sno from student where ssex="男");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值