From 3aceed331fd8bac94a6535b9d05c4216f4b6f51b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=86=8A=E6=99=AF=E5=B3=B0?= <1532577707@qq.com> Date: Sun, 5 Mar 2023 12:39:33 +0800 Subject: [PATCH 1/2] 20230305 --- .../20230305\344\275\234\344\270\232.md" | 216 ++++++++++++++++++ ...46\346\235\237\347\254\224\350\256\260.md" | 0 ...45\350\257\242\347\254\224\350\256\260.md" | 0 3 files changed, 216 insertions(+) create mode 100644 "33 \347\206\212\346\231\257\345\263\260/20230305\344\275\234\344\270\232.md" rename "33 \347\206\212\346\231\257\345\263\260/2023221\347\254\224\350\256\260.md" => "33 \347\206\212\346\231\257\345\263\260/2023221\347\272\246\346\235\237\347\254\224\350\256\260.md" (100%) rename "33 \347\206\212\346\231\257\345\263\260/2023225\347\254\224\350\256\260.md" => "33 \347\206\212\346\231\257\345\263\260/2023225\346\237\245\350\257\242\347\254\224\350\256\260.md" (100%) diff --git "a/33 \347\206\212\346\231\257\345\263\260/20230305\344\275\234\344\270\232.md" "b/33 \347\206\212\346\231\257\345\263\260/20230305\344\275\234\344\270\232.md" new file mode 100644 index 0000000..4455da7 --- /dev/null +++ "b/33 \347\206\212\346\231\257\345\263\260/20230305\344\275\234\344\270\232.md" @@ -0,0 +1,216 @@ +```mysql +create database test charset utf8; +use test; +create table student( +Sno varchar(20) primary key comment'学号', +Sname varchar(20)not null comment'学生姓名', +Ssex varchar(20)not null comment'学生性别', +Sbirthday datetime comment'学生出生年月', +Class varchar(20) comment'学生所在班级' +); +create table course( +Cno varchar(20) primary key comment'课程号', +Cname varchar(20)not null comment'课程名称', +Tno varchar(20)not null comment'教工编号', +foreign key (Tno) references Teacher(Tno) +); +create table score( +Sno varchar(20)not null comment'学号', +Cno varchar(20)not null comment'课程号', +Degree decimal(4,1) comment'成绩', +primary key (Sno,Cno), +foreign key (Sno) references Student(Sno), +foreign key (Cno) references Course(Cno) +); +create table teacher( +Tno varchar(20)primary key comment'教工编号', +Tname varchar(20)not null comment'教工姓名', +Tsex varchar(20)not null comment'教工性别', +Tbirthday datetime comment'教工出生年月', +Prof varchar(20) comment'职称', +Depart varchar(20)not null comment'教工所在部门' +); +insert into student values +('108','曾华','男','1977-9-1','95033'), +('105','匡明','男','1975-10-2','95031'), +('107','王丽','女','1976-1-23','95033'), +('101','李军','男','1976-2-20','95033'), +('109','王芳','女','1975-2-10','95031'), +('103','陆君','男','1974-6-3','95031'); +insert into course values +('3-105','计算机导论','825'), +('3-245','操作系统','804'), +('6-166','数字电路','856'), +('9-888','高等数学','831'); +insert into score values +('103','3-245','825'), +('105','3-245','804'), +('109','3-245','856'), +('103','3-105','831'), +('105','3-105','825'), +('109','3-105','804'), +('101','3-105','856'), +('107','3-105','825'), +('108','3-105','804'), +('101','6-166','856'), +('107','6-166','825'), +('108','6-166','804'); +insert into teacher values +('804','李诚','男','1958-12-2','副教授','计算机系'), +('856','张旭','男','1969-3-12','讲师','电子工程系'), +('825','王萍','女','1972-5-5','讲师','计算机系'), +('831','刘冰','女','1977-8-14','讲师','电子工程系'); +``` + +-- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息 + +```mysql +select st.Sname,co.Cname from student st,course co,score sc where st.Sno=sc.Sno and co.Cno=sc.Cno; +``` + +-- 2,查询没有学生的教师的所有信息 + +```mysql +select * from teacher te left join course co on te.Tno=co.Tno left join score sc on co.Cno=sc.Cno where sc.Cno is null; +``` + +① 查询Score表中的最高分的学生学号和课程号。 + +```mysql +select Sno,Cno from score where Degree=(select max(Degree) from score); +``` + +② 查询所有学生的Sname、Cno和Degree列。 + +``` +select st.Sname,sc.Cno,sc.Degree from student st ,score sc where st.sno=sc.sno; +``` + +③ 查询所有学生的Sno、Cname和Degree列。 + +```mysql +select st.Sno,st.Sname,sc.Degree from student st ,score sc where st.sno=sc.sno; +``` + +④ 查询所有学生的Sname、Cname和Degree列。 + +```mysql +select st.Sname,co.Cname,sc.Degree from student st ,course co ,score sc where st.sno=sc.sno and co.Cno=sc.Cno; +``` + +⑤ 查询“95033”班学生的平均分。 + +```mysql +select avg(sc.Degree) from student st, score sc where st.Sno=sc.Sno and Class='95033'; +``` + +⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 + +```mysql +select * from score where Degree>(select Degree from score where Sno='109'and Cno='3-105') and Cno='3-105'; +``` + +⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 + +```mysql +select * from score where Degree not in (select max(Degree) from score group by Cno); +``` + +⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 + +```mysql +select * from score where Degree>(select Degree from score where Sno='109' and Cno='3-105'); +``` + +⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 + +```mysql +select Sno,Sname,Sbirthday from student where year(Sbirthday)=(select year(Sbirthday) from student where Sno='108') and Sno!='108'; +``` + +⑩ 查询“张旭“教师任课的学生成绩。 + +```mysql +select Degree from teacher te,score sc,course co where te.Tno= co.Tno and co.Cno=sc.Cno and tname='张旭'; +``` + +⑪ 查询选修某课程的同学人数多于5人的教师姓名。 + +```mysql +select tname from course co,teacher te where co.Tno=te.Tno and Cno=(select Cno from score group by Cno having count(*)>5); +``` + +⑫ 查询出“计算机系“教师所教课程的成绩表。 + +```mysql +select sc.* from course co, score sc where co.Cno = sc.Cno and co.Tno in(select Tno from teacher where Depart='计算机系'); +``` + +⑬ 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 + +```mysql +select Tname,Prof from teacher where Prof not in (select Prof from teacher where Depart='计算机系') +union all +select Tname,Prof from teacher where Prof not in (select Prof from teacher where Depart='电子工程系'); +``` + +⑭ 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 + +```mysql +select Cno,Sno,Degree from score where Cno='3-105' and Degree>all(select Degree from score where Cno='3-245') order by Degree desc; +``` + +⑮ 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. + +```mysql +select * from score where Cno='3-105' and Degree>all(select Degree from score where Cno='3-245'); +``` + +⑯ 查询成绩比该课程平均成绩低的同学的成绩表。 + +```mysql +select * from score a where a.Degree<(select avg(Degree) from score b where a.Cno=b.Cno); +``` + +⑰ 查询所有任课教师的Tname和Depart. + +```mysql +select Tname,Depart from teacher; +``` + +查询所有未讲课的教师的Tname和Depart. + +```mysql +select te.tname,te.Depart from teacher te left join course co on te.Tno = co.Tno left join score sc on co.Cno = sc.Cno where sc.Sno is null ; +``` + +⑲ 查询“男”教师及其所上的课程。 + +```mysql +select * from teacher te,course co where te.tno=co.Tno and Tsex='男'; +``` + +⑳ 查询最高分同学的Sno、Cno和Degree列。 + +```mysql +select sno,Cno,Degree from score where Cno in(select Cno from score group by Cno) and Degree in (select max(Degree) from score group by Cno); +``` + +21 查询和“李军”同性别的所有同学的Sname. + +```mysql +select Sname from student where Ssex=(select Ssex from student where Sname='李军'); +``` + +22 查询和“李军”同性别并同班的同学Sname. + +```mysql +select Sname from student where Ssex=(select Ssex from student where Sname='李军') and Class=(select class from student where Sname='李军'); +``` + +23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 + +```mysql +select * from student st , score sc ,course co where st.Sno=sc.Sno and co.Cno=sc.Cno and st.Ssex='男' and co.Cname='计算机导论'; +``` + diff --git "a/33 \347\206\212\346\231\257\345\263\260/2023221\347\254\224\350\256\260.md" "b/33 \347\206\212\346\231\257\345\263\260/2023221\347\272\246\346\235\237\347\254\224\350\256\260.md" similarity index 100% rename from "33 \347\206\212\346\231\257\345\263\260/2023221\347\254\224\350\256\260.md" rename to "33 \347\206\212\346\231\257\345\263\260/2023221\347\272\246\346\235\237\347\254\224\350\256\260.md" diff --git "a/33 \347\206\212\346\231\257\345\263\260/2023225\347\254\224\350\256\260.md" "b/33 \347\206\212\346\231\257\345\263\260/2023225\346\237\245\350\257\242\347\254\224\350\256\260.md" similarity index 100% rename from "33 \347\206\212\346\231\257\345\263\260/2023225\347\254\224\350\256\260.md" rename to "33 \347\206\212\346\231\257\345\263\260/2023225\346\237\245\350\257\242\347\254\224\350\256\260.md" -- Gitee From 32091285b831f6c8590fbf06efbe7fa0db378ee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=86=8A=E6=99=AF=E5=B3=B0?= <1532577707@qq.com> Date: Tue, 7 Mar 2023 23:29:14 +0800 Subject: [PATCH 2/2] 20230307 --- .../20230307\344\275\234\344\270\232.md" | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 "33 \347\206\212\346\231\257\345\263\260/20230307\344\275\234\344\270\232.md" diff --git "a/33 \347\206\212\346\231\257\345\263\260/20230307\344\275\234\344\270\232.md" "b/33 \347\206\212\346\231\257\345\263\260/20230307\344\275\234\344\270\232.md" new file mode 100644 index 0000000..ac8fd3b --- /dev/null +++ "b/33 \347\206\212\346\231\257\345\263\260/20230307\344\275\234\344\270\232.md" @@ -0,0 +1,150 @@ +```mysql +drop database if exists work; +create database work charset utf8; +use work; +create table stuinfo( +stuno varchar(20) primary key, +stuname varchar(20), +stusex char(1), +stuage int, +stuaddress varchar(20), +stuseat int +); +create table stuexam( +examno int primary key , +stuno varchar(20), +writtenexam int, +labexam int +); +create table stumarks( +examno int, +stuid varchar(20), +score int +); +alter table stuexam add foreign key (stuno) references stuinfo(stuno); +alter table stumarks add foreign key(examno) references stuexam(examno); +alter table stumarks add foreign key(stuid) references stuinfo(stuno); +insert into stuinfo values +('s2501','张秋利','男','20','美国硅谷','1'), +('s2502','李斯文','女','18','湖北武汉','2'), +('s2503','马文才','男','22','湖南长沙','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'); +update stuinfo set stuage=18 where stuname='马文才'; +``` + +在如图的数据表上完成以下题目 + +1.查询出年龄比班上平均年龄大的学生的信息 + +```mysql +select * from stuinfo where stuage> +(select avg(stuage) from stuinfo); +``` + +2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) + +```mysql +select stuno,stusex,stuname,max(score) from stuinfo a left join stumarks b on a.stuno=b.stuid + where stuid is not null group by stuno; +``` + +3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) + +```mysql +select stuinfo.stuno,stuname,stusex,((writtenexam+labexam)/2) 平均值 from stuinfo +left join stuexam on stuinfo.stuno=stuexam.stuno; +``` + +4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) + +```mysql +select * from stuinfo where stusex='男' and stuage>=20; +select * from stuinfo where stuage = any (select stuage from stuinfo where stuage>=20) + and stusex = any (select stusex from stuinfo where stusex='男'); +``` + +5.查询出年龄比所有男生年龄都大的女生的信息 + +```mysql +select * from stuinfo where stuage>(select max(stuage) from stuinfo where stusex='男') and stusex='女'; +``` + +6.查询出所有选修课程都及格的学生的信息 (stuMarks) + +```mysql +select * from stuinfo a,(select min(score) c,stuid from stumarks group by stuid) b +where a.stuno=b.stuid and c>=60; +``` + +7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +```mysql +select * from stuinfo a,(select distinct stuid from stumarks) b + where a.stuno=b.stuid; +``` + +8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +```mysql +select * from stuinfo a left join stumarks b on a.stuno=b.stuid + where stuid is null ; +``` + +9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) + +```mysql +select * from stuinfo a inner join stumarks b on a.stuno=b.stuid + where b.score>=90; +``` + +10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) + +```mysql +select * from stuinfo a,(select stuid,avg(score) c from stumarks group by stuid) b +where a.stuno=b.stuid and c>=80; +``` + +11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) + +```mysql +select a.* from stuinfo a, (select stuid from stumarks where stuid!='s2501' group by stuid having max(score)>(select max(score) from stumarks where stuid='s2501')) b +where a.stuNO=b.stuID; +``` + +12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) + +```mysql +select a.* from stuinfo a, (select stuid from stumarks where stuid!='s2501' group by stuid having max(score)>(select min(score) from stumarks where stuID='s2501')) b +where a.stuNO=b.stuID; +``` + +13.查询班上比所有男生年龄都要大的女生的信息 + +```mysql +select * from stuinfo where stusex='女' and stuage>(select max(stuage) from stuinfo where stusex='男'); +``` + + + +14.查询出只是比某个男生年龄大的女生的信息 + +```mysql +select * from stuinfo where stusex='女' and stuage>(select min(stuage) from stuinfo where stusex='男'); +``` + -- Gitee