diff --git "a/12 \345\217\266\344\277\212\350\275\266/20230309 \347\273\203\344\271\240.md" "b/12 \345\217\266\344\277\212\350\275\266/20230309 \347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..1d3e104175cdcff3e8c86f88d959381d11a4788c --- /dev/null +++ "b/12 \345\217\266\344\277\212\350\275\266/20230309 \347\273\203\344\271\240.md" @@ -0,0 +1,77 @@ +CREATE TABle stuinfo ( +stuno char(5) PRIMARY key, +stuname varchar(10), +stusex enum('男','女'), +stuage INT, +stuaddress varchar(4), +stuseat int); + +CREATE table stuexam( +examno int PRIMARY key, +stuno char(5), +writtenexam int, +labexam int, +FOREIGN key stuexam(stuno) REFERENCES stuinfo(stuno)); + +CREATE TABLE stumarks( +examno int, +stuid char(5) REFERENCES stuexam(stuno), +score int, +FOREIGN KEY stumarks(examno) REFERENCES stuexam(examno)); + +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 stuno,stuname,stusex,max(score) from stuinfo LEFT JOIN stumarks on stuinfo.stuno = stumarks.stuid GROUP BY stuid +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +SELECT stuinfo.stuname,stuinfo.stuno,(writtenexam+labexam)/2 from stuexam right JOIN stuinfo on stuexam.stuno = stuinfo.stuno +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +SELECT * from stuinfo where stusex = '男' and stuage >= 20; +SELECT * from (SELECT * from stuinfo) nb where stusex = '男' and stuage >= 20; + +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +SELECT max(stuage) from stuinfo where stusex = '男' +SELECT * from stuinfo where stusex = '女' and stuage > (SELECT max(stuage) from stuinfo where stusex = '男') +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +SELECT stuinfo.* from stuinfo right JOIN (SELECT * from stumarks where stuid !=(SELECT stuid from stumarks where score <60)) nb on stuinfo.stuno = nb.stuid GROUP BY stuno +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) + +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +-- +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +-- +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +-- +-- 13.查询班上比所有男生年龄都要大的女生的信息 +-- +-- 14.查询出只是比某个男生年龄大的女生的信息 +-- \ No newline at end of file