diff --git "a/05 \350\226\233\347\245\226\344\277\241/20230306 \345\255\220\346\237\245\350\257\242.md" "b/05 \350\226\233\347\245\226\344\277\241/20230306 \345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..d83bd885bd1dd50ff66a25b980ec8e17ef89dc36 --- /dev/null +++ "b/05 \350\226\233\347\245\226\344\277\241/20230306 \345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,95 @@ +# 1. 学习 + +```sql +select 字段(子查询) from 表 + +select 字段 from 条件 (子查询) + +select 字段 from (子查询) as 别名 +``` + + + +# 2. 作业 + +```sql +-- 在如图的数据表上完成以下题目 +-- +create database xzx charset utf8; +use xzx; +create table stuinfo( + stuno varchar(10) primary key, + stuname varchar(10), + stusex enum('男','女'), + stuage INT, + stuaddress varchar(100), + stuseat int +); + +create table stuexam( + examno int primary key, + stuno varchar(10), + writtenexam int, + labexam int +); + +create table stumarks( + examno int primary key, + stuid varchar(10), + score int +); +alter table stuexam add foreign key (stuno) references stuinfo(stuno); + +alter table stumarks add foreign key(examno) references stuexam(examno) on update cascade on delete cascade; + +alter table stumarks add foreign key(stuid) references stuinfo(stuno) on update cascade on delete cascade; + + +select * from stuinfo; +desc stuinfo; +insert into stuinfo values('s2501','张秋利','男',20,'美国硅谷',1),('s2502','李斯文','女',18,'湖北武汉',2),('s2503','马文才','男',18,'湖南长沙',3),('s2504','欧阳俊雄','女',21,'湖北武汉',4),('s2505','梅超风','男',20,'湖北武汉',5),('s2506','陈旋风','男',19,'美国硅谷',6); + +desc stuexam; +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); + +desc stumarks; +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 x.stuno,x.stuname,stusex,max(score) from stuinfo x left join stumarks y on x.stuno=y.stuid GROUP BY stuname; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) + select x.stuno,x.stuname,x.stusex,(writtenexam+labexam)/2 平均分 from stuinfo x inner join stuexam y on x.stuno=y.stuno; +-- +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) + select * from stuinfo where stusex='男' and stuage>=20; + select * from (select * from stuinfo where stusex='男' and stuage>=20) a; +-- 5.查询出年龄比所有男生年龄都大的女生的信息 + select * from stuinfo where stuage>(select max(stuage) from stuinfo where stusex='男') +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) + select * from stuinfo x left join stumarks y on x.stuno=y.stuid where stuid not in (select stuid from stumarks where score <60) GROUP BY stuname; +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + select * from stuinfo inner join stumarks on stuinfo.stuno=stumarks.stuid GROUP BY stuname; + select * from stuinfo where stuno in(select stuid from stumarks); +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + select * from stuinfo left join stumarks on stuinfo.stuno=stumarks.stuid where stuid is null; + select * from stuinfo where stuno NOT in (select stuid from stumarks); +-- 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 score>(select avg(score) from stumarks); +-- +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) + select * from stuinfo inner join stumarks on stuinfo.stuno=stumarks.stuid where score > (select max(score) from stumarks where stuid=(select stuno from stuinfo where stuname='张秋利')); +-- +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) + select * from stuinfo inner join stumarks on stuinfo.stuno=stumarks.stuid where score > any (select score from stumarks where stuid=(select stuno from stuinfo where stuname='张秋利')) and stuname!='张秋利'; +-- +-- 13.查询班上比所有男生年龄都要大的女生的信息 + select * from stuinfo where stuage>(select max(stuage) from stuinfo where stusex='男') +-- 14.查询出只是比某个男生年龄大的女生的信息 + select * from stuinfo where stuage >= any (select stuage from stuinfo where stusex='男') and stusex!='男'; +-- -- +``` +