From 81d040b85a587e8eddb3126c4c700ffbbf00dff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E5=80=A9?= <2931430074@qq.com> Date: Tue, 7 Mar 2023 21:45:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=99=E5=80=A9=E7=9A=84=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6 \345\255\220\346\237\245\350\257\242.md" | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 "24 \344\275\231\345\200\251/20230306 \345\255\220\346\237\245\350\257\242.md" diff --git "a/24 \344\275\231\345\200\251/20230306 \345\255\220\346\237\245\350\257\242.md" "b/24 \344\275\231\345\200\251/20230306 \345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..61e0112 --- /dev/null +++ "b/24 \344\275\231\345\200\251/20230306 \345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,76 @@ +# 一,笔记 + +``` +select ① from ③ where ②; +# 形式一,select后面嵌套子查询 where 后面不支持聚合函数和别名,having支持聚合也支持别名 +# 形式二,在where/having 后面做条件 +# 形式三,在from后面,形成一个临时表,必须加一个别名 + + 子查询的结果: + 单列单个值:可以放在select 后,也可以放where/having之后 + 单列多个值:where后,或having后 ,不用直接用<,=,>= 这种单纯的比较运算符。但可以用搭配any(任意一个),all(所有的)等关键字一起使用。还可以用in,not in这种表达式 + 多列时,只能当临时表来表用,放在from后面,而且必须,给他取个别名 +``` + + + +# 二,作业 + +```sql +create database school charset utf8; +use school; +create TABLE stuinfo( +stuno VARCHAR(5) primary key, +stuname varchar(10), +stusex enum('男','女'), +stuage int, +stuadress VARCHAR(20), +stuseat int); +create table stuexam( +examno int PRIMARY key, +stuno VARCHAR(5), +writtenexam float, +labexam FLOAT, +FOREIGN key stuexam(stuno) REFERENCES stuinfo(stuno) +); +CREATE table stumarks( +examno int REFERENCES stuexam(examno), +stuid VARCHAR(5) REFERENCES stuinfo(stuno), +score float +); + +INSERT stuinfo VALUES('s2501','张秋利','男',20,'美国硅谷',1),('s2502','李斯文','女',18,'湖北武汉',2),('s2503','马文才','男',18,'湖南长沙',3),('s2504','欧阳俊雄','女',21,'湖北武汉',4),('s2505','梅超风','男',20,'湖北武汉',5),('s2506','陈旋风','男',19,'美国硅谷',5); +insert 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 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 stuno,stuname,stusex,max(stumarks.score)from stuinfo inner join stumarks on stuinfo.stuno = stumarks.stuid GROUP BY stuno; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +SELECT stuinfo.stuno,stuname,stusex,(writtenexam+labexam)/2 from stuinfo inner join stuexam on stuinfo.stuno = stuexam.stuno; +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +SELECT * from stuinfo where stusex='男' and stuage >= 20; +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +SELECT * from stuinfo where stusex='女' and stuage > (SELECT max(stuage) from stuinfo where stusex='男') +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +SELECT * from stuinfo inner join stumarks on stuinfo.stuno=stumarks.stuid WHERE stuinfo.stuno !=(SELECT stuid FROM stumarks WHERE score < 60 ); +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +SELECT * from stuinfo inner join stumarks on stuno in(SELECT stuid from stumarks); +SELECT * from stuinfo inner join stumarks on stuinfo.stuno=stumarks.stuid; +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +SELECT * from stuinfo left join stumarks on stuinfo.stuno=stumarks.stuid where stuid is null; +SELECT * from stuinfo LEFT JOIN stumarks on stuno in(SELECT stuid from stumarks) where stuid is null; +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +SELECT * from stuinfo inner join stumarks on stuinfo.stuno=stumarks.stuid where score > 90; +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +SELECT * from stuinfo inner join stumarks on stuinfo.stuno=stumarks.stuid WHERE stuinfo.stuno !=(SELECT stuid FROM stumarks WHERE avg(score) < 80 ); +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +SELECT * from stuinfo left join stumarks on stuinfo.stuno=stumarks.stuid where score >(SELECT max(score) from stumarks left join stuinfo on stumarks.stuid=stuinfo.stuno where stuinfo.stuname='张秋利'); +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +SELECT * from stuinfo left join stumarks on stuinfo.stuno=stumarks.stuid where score > all(SELECT score from stumarks where stuinfo.stuname='张秋利'); +-- 13.查询班上比所有男生年龄都要大的女生的信息 +SELECT * from stuinfo where stusex='女' and stuage>(SELECT max(stuage) from stuinfo where stusex='男'); +-- 14.查询出只是比某个男生年龄大的女生的信息 +SELECT * from stuinfo where stusex='女' and stuage > any(SELECT stuage from stuinfo where stusex='男'); +``` + -- Gitee