diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230306 \345\255\220\346\237\245\350\257\242.md" "b/10 \347\253\240\347\254\221\345\256\271/20230306 \345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..170665d323e6a731f5e77c63594a3cc3451d06fb --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/20230306 \345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,123 @@ +# 1、笔记 + +```sql +-- 什么子查询 +-- 嵌套在其它SQL语句中的select语句 +-- 子查询通常在外层语句之前优先执行,所以要用()包括起来 +-- 子查询的结果,一般作为外层语句的条件,数据源等 +-- 形式一,select后面嵌套子查询 +select (子查询) from 表名 [where/having 条件]; +-- 形式二,在where/having 后面做条件 +select 字段名 from 表名 where/having (子查询); + +-- 当子查询放在where 、 having 后面当条件用时: +-- 单列单个值,可以直接用>,<,<=...!=,这些比较运算符 +-- 单列多个值,要用in,not in 这种集合的比较 +-- 单列多个值,可以用>,<,<=...!=,比较运算符,搭配any,all 这些关键字,一起使用 + + + +-- 形式三,在from后面,形成一个临时表,必须加一个别名 +select 字段名 from (子查询) [as] 别名 [where/having 条件]; + + +-- 子查询的结果: +-- 单列单个值:可以放在select 后,也可以放where/having之后 +-- 单列多个值:where后,或having后 ,不用直接用<,=,>= 这种单纯的比较运算符。但可以用搭配any(任意一个),all(所有的)等关键字--- 一起使用。还可以用in,not in这种表达式 +-- 多列时,只能当临时表来表用,放在from后面,而且必须,给他取个别名 +``` + + + +# 2、作业 + +```sql +use mockq; +create table stuinfo( + stuno varchar(10), + stuname varchar(5), + stusex enum('男','女'), + stuage varchar(5), + stuaddress varchar(10), + stuseat int +); +create table stuexam( + examno int, + stuno varchar(10), + whitenexam varchar(5), + labexam varchar(5) +); +create table stumarks( + examno int, + stuid varchar(10), + score varchar(5) +); +insert into stuinfo values + ('s2501','张秋利','男','20','美国硅谷',1), + ('s2502','李斯文','女','18','湖北武汉',2), + ('s2503','马文才','男','18','湖南长沙',3), + ('s2504','欧阳俊雄','女','21','湖北武汉',4), + ('s2505','梅超风','男','20','湖北武汉',5), + ('s2506','陈旋风','男','19','美国硅谷',6); +insert into stuexam VALUES + (1,'s2501','50','70'), + (2,'s2502','60','65'), + (3,'s2503','86','70'), + (4,'s2504','40','80'), + (5,'s2505','70','85'), + (6,'s2506','85','90'); +insert into stumarks VALUES + (1,'s2501','88'), + (2,'s2501','92'), + (3,'s2501','53'), + (4,'s2502','60'), + (5,'s2502','99'), + (6,'s2503','82'); +-- 在如图的数据表上完成以下题目 +-- +-- 1.查询出年龄比班上平均年龄大的学生的信息 +select * from stuinfo where stuage > (select avg(stuage) from stuinfo); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +-- select a.stuno,a.stuname,a.stusex,max(b.score) +-- from stuinfo as a left join stumarks as b on a.stuno = b.stuid group by a.stuno; +select b.stuno,b.stuname,b.stusex,max(a.score) from stumarks as a right join stuinfo as b on a.stuid = b.stuno group by b.stuno; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select a.stuno,a.stuname,a.stusex,(b.whitenexam + b.labexam)/2 as '考试平均分' from stuinfo as a left join stuexam as b on a.stuno = b.stuno group by a.stuno; +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stusex = '男' and stuage >= 20; +select stuinfo.* from stuinfo right join +(select * from stuinfo where stuage >= 20) as age on stuinfo.stuno = age.stuno where age.stusex = '男'; +-- 5.查询出年龄比所有男生年龄都大的女生的信息 + +select * from stuinfo where stuage > (select max(stuage) from stuinfo where stusex = '男') and stusex = '女'; +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +-- select * from stumarks where score > 60 and +SELECT * +from stuinfo as a inner join stumarks as b on a.stuno = b.stuid where score > 60 ; +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select * from stuinfo as a right join stumarks as b on a.stuno = b.stuid; +select * from stuinfo where stuno in (select stuno from stuinfo as a right join stumarks as b on a.stuno = b.stuid); +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select * from stuinfo as a left join stumarks as b on a.stuno = b.stuid where b.stuid is null; +select * from stuinfo where stuno not in (select stuno from stuinfo as a right join stumarks as b on a.stuno = b.stuid); +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select * from stumarks where score > 90; +select * from stuinfo as a inner join (select * from stumarks where score > 90) as b on a.stuno = b.stuid; +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select avg(score) from stumarks group by stuid having avg(score) > 80; +select stuinfo.* +from (select stuid,avg(score) from stumarks group by stuid having avg(score) > 80) as a inner join stuinfo on a.stuid = stuinfo.stuno; +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select max(score) from stumarks where stuid = (select stuno from stuinfo where stuname = '张秋利'); +select a.* +from stuinfo as a inner join stumarks as b on a.stuno = b.stuid where b.score > (select max(score) from stumarks where stuid = (select stuno from stuinfo where stuname = '张秋利')); +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select min(score) from stumarks where stuid = (select stuno from stuinfo where stuname = '张秋利'); +select a.* +from stuinfo as a inner join stumarks as b on a.stuno = b.stuid where b.score > (select min(score) from stumarks where stuid = (select stuno from stuinfo where stuname = '张秋利')) group by a.stuno; +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo where stuage > (select max(stuage) from stuinfo where stusex = '男') and stusex = '女'; +-- 14.查询出只是比某个男生年龄大的女生的信息 +select * from stuinfo where stuage > (select min(stuage) from stuinfo where stusex = '男') and stusex = '女'; +``` +