diff --git "a/40 \346\226\207\346\231\272\345\213\207/20230216 .md" "b/40 \346\226\207\346\231\272\345\213\207/20230216.md" similarity index 100% rename from "40 \346\226\207\346\231\272\345\213\207/20230216 .md" rename to "40 \346\226\207\346\231\272\345\213\207/20230216.md" diff --git "a/40 \346\226\207\346\231\272\345\213\207/20230219 MySQL.md" "b/40 \346\226\207\346\231\272\345\213\207/20230219MySQL.md" similarity index 100% rename from "40 \346\226\207\346\231\272\345\213\207/20230219 MySQL.md" rename to "40 \346\226\207\346\231\272\345\213\207/20230219MySQL.md" diff --git "a/40 \346\226\207\346\231\272\345\213\207/20230221 mysql\347\272\246\346\235\237.md" "b/40 \346\226\207\346\231\272\345\213\207/20230221mysql\347\272\246\346\235\237.md" similarity index 100% rename from "40 \346\226\207\346\231\272\345\213\207/20230221 mysql\347\272\246\346\235\237.md" rename to "40 \346\226\207\346\231\272\345\213\207/20230221mysql\347\272\246\346\235\237.md" diff --git "a/40 \346\226\207\346\231\272\345\213\207/20230223 MySQL\344\275\234\344\270\232.md" "b/40 \346\226\207\346\231\272\345\213\207/20230223MySQL\344\275\234\344\270\232.md" similarity index 100% rename from "40 \346\226\207\346\231\272\345\213\207/20230223 MySQL\344\275\234\344\270\232.md" rename to "40 \346\226\207\346\231\272\345\213\207/20230223MySQL\344\275\234\344\270\232.md" diff --git "a/40 \346\226\207\346\231\272\345\213\207/20230236\345\255\220\346\237\245\350\257\242.md" "b/40 \346\226\207\346\231\272\345\213\207/20230236\345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..0ca9bd11446cf78170b13b7a8da3d64aee871d69 --- /dev/null +++ "b/40 \346\226\207\346\231\272\345\213\207/20230236\345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,101 @@ +```mysql +create database class3 charset utf8; +use class3; +-- drop table stuinfo; +create table stuinfo( +stuNO varchar(10) primary key, +stuName varchar(10), +stuSex varchar(10), +stuAge varchar(10), +stuAddress varchar(20), +stuSeat int(10) +); +-- drop table stuExam; +CREATE table stuExam( +examNo int primary key auto_increment, +stuNO VARCHAR(10), +writtenExam int, +labExam INT +); +-- drop table stuMarks; +CREATE table stuMarks( +examNo int primary key auto_increment, +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); +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(score) from stuinfo a left join stumarks b on a.stuNO=b.stuID group by stuNO; + +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +-- +select a.stuNO,a.stuName,a.stuSex,(b.writtenExam+b.labExam)/2 平均分 from stuinfo a left join stuexam b on a.stuNo=b.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 a.* from stuinfo a,(select stuID from stumarks group by stuID having min(score)>=60) b where a.stuNO=b.stuID; + +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.*from stuinfo a,(select stuID from stuMarks group by stuID ) b where a.stuNO=b.stuID; + +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select *from stuinfo where stuno not in(select stuID from stuMarks group by stuID); + +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select a.*from stuinfo a,(select stuID from stuMarks group by stuID having max(score)>90) b where a.stuNO=b.stuID; + +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select a.*from stuinfo a,(select stuID from stuMarks group by stuID having avg(score)>80) b where a.stuNO=b.stuID; + + +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +-- +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) +-- +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')) bwhere a.stuNO=b.stuID; +-- 13.查询班上比所有男生年龄都要大的女生的信息 +-- + +select * from stuinfo where stuSex='女' and stuAge>(select max(stuAge) from stuinfo where stuSex='男'); +-- 14.查询出只是比某个男生年龄大的女生的信息 +-- +select * from stuinfo where stuSex='女' and stuAge>(select min(stuAge) from stuinfo where stuSex='男'); +-- +-- +``` + diff --git "a/40 \346\226\207\346\231\272\345\213\207/20230305MYSQL\344\275\234\344\270\232.md" "b/40 \346\226\207\346\231\272\345\213\207/20230305MYSQL\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..110ca6370b60b4194f9e040df4fb6884560a2a3c --- /dev/null +++ "b/40 \346\226\207\346\231\272\345\213\207/20230305MYSQL\344\275\234\344\270\232.md" @@ -0,0 +1,154 @@ +```mysql +create database mxdx default charset utf8; +use mxdx; +-- 第一个表格 +create table Student ( + Sno varchar(20) primary key , + Sname varchar(20) not null , + Ssex varchar(20) not null , + Sbirthday datetime , + Class varchar(20) +) comment '学生表'; +-- 第二个表格 +create table Course( + Cno varchar(20) primary key , + Cname varchar(20) not null , + Tno varchar(20) not null, + foreign key Course(Tno) references teacher(Tno) +) comment '课程表'; +-- 第三个表格 +create table Score( + Sno varchar(20) not null , + Cno varchar(20) not null , + Degree decimal(4,1), + primary key (sno,cno), + foreign key (Sno) references student(Sno), + foreign key (Cno) references course(Cno) +)comment '成绩表'; +-- 第四个表格 +create table Teacher( + Tno varchar(20) primary key , + Tname varchar(20) not null , + Tsex varchar(20) not null , + Tbirthday datetime , + Prof varchar(20), + Depart varchar(20)not null +)comment '教师表'; +insert into student value + ('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 value + ('3-105','计算机导论','825'), + ('3-245','操作系统','804'), + ('6-166','数学电路','856'), + ('9-888','高等数学','831'); +insert into score value + ('103','3-245',86), + ('105','3-245',75), + ('109','3-245',68), + ('103','3-105',92), + ('105','3-105',88), + ('109','3-105',76), + ('101','3-105',64), + ('107','3-105',91), + ('108','3-105',78), + ('101','6-166',85), + ('107','6-166',79), + ('108','6-166',81); +insert into teacher value + ('804','李诚','男','1958-12-2','副教授','计算机系'), + ('856','张旭','男','1969-3-12','讲师','电子工程系'), + ('825','王萍','女','1972-5-5','助教','计算机系'), + ('831','刘冰','女','1977-8-14','助教','电子工程系'); +-- 查询所有学生,都学了哪些课程,要显示学生信息和课程信息 +select s.*,Course.* from + student s inner join score on s.Sno=Score.Sno + inner join course on Score.Cno=Course.Cno; +-- 第二个方法 要连接3个表 一个学生表 一个课程表 一个成绩表 +-- 连接条件是 student.sno = score.sno and score.con = course.con +select s.* ,c.* from student s ,score se ,course c where s.Sno = se.Sno and se.cno = c.Cno; +-- 查询没有学生的教师的所有信息 +select distinct t.* from + teacher t + left join course on t.Tno=Course.Tno + left join score on Course.Cno=Score.Cno + left join student on Score.Sno=Student.Sno where Score.Sno is null ; +-- 第二个个方法 连接三个表 teacher course score 教师表 成绩表 课程表 +-- 连接方法 teacher.tno=course.tno and course.cno=score.cno and course.cno +select * from teacher left join Course C on Teacher.Tno = C.Tno +left join score s on C.Cno = s.Cno +where Sno is null ; + +-- 查询Score表中的最高分的学生学号和课程号。 +select Sno,Cno,Degree from Score where Degree=(select (max(Degree)) from Score); +select max(Degree) from score; +select * from score where Degree =(select max(Degree) from score); + +-- 查询所有学生的Sname、Cno和Degree列。 +-- 连接二个表 学生表 成绩表 s.Sno=se.Sno +select s.Sname ,se.Cno,se.Degree from student s ,score se where s.Sno=se.Sno; +-- 查询所有学生的Sno、Cname和Degree列。 +select s.Sno,c.Cname,s.Degree from score s,course c where s.Cno=c.Cno ; +-- 查询所有学生的Sname、Cname和Degree列。 +select s.Sname,c.Cname,se.Degree from score se ,course c,student s where se.cno=c.cno and se.sno=s.sno; +-- 查询“95033”班学生的平均分。 +select Class,avg(Degree) 平均分 from student s,score se where s.sno=se.sno and Class='95033'; +-- 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select * from student s,score se where s.Sno=se.Sno and se.cno='3-105'and Degree > all (select Degree from score where sno='109'); +-- 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +(select max(degree) from score); +select Degree from score where Degree != (select max(degree) from score)group by Degree; +-- 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from student s,score se where s.Sno=se.Sno and Degree> (select Degree from score where sno='109' and cno='3-105'); +-- 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select year(Sbirthday) from student where sno='108'; +select sno ,Sname,Sbirthday from student where year(Sbirthday)=(select year(Sbirthday) from student where sno='108'); +-- 查询“张旭“教师任课的学生成绩。 +select * from teacher where Tname='张旭'; +select Degree from teacher t ,course c ,score s where t.Tno=c.Tno and c.Cno=s.cno and t.Tname='张旭'; +-- 查询选修某课程的同学人数多于5人的教师姓名。 +select s.Cno from teacher t ,course c ,score s where t.Tno=c.Tno and c.Cno=s.cno group by c.Cno having count(s.Cno)>5; +-- 查询出“计算机系“教师所教课程的成绩表。 +select s.Cno,s.Degree from teacher t,course c,score s where t.tno=c.tno and c.Cno=s.Cno and t.Depart='计算机系'; +-- 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select tname ,prof from teacher where Depart='计算机系' +union +select tname ,prof from teacher where Depart='电子工程系'; + +-- 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select Degree from score where Cno='3-245'; +select * 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. +select * from score where Cno='3-105' and Degree > all (select Degree from score where Cno='3-245'); + +-- 查询成绩比该课程平均成绩低的同学的成绩表。 +select avg(Degree) from score; +select Degree from score where Degree< (select avg(Degree) from score); + +-- 查询所有任课教师的Tname和Depart. +select Tname,Depart from teacher; + +-- 查询所有未讲课的教师的Tname和Depart. +select Tname,Depart from teacher left join Course C on Teacher.Tno = C.Tno left join score s on C.Cno = s.Cno where Sno is null ; + +-- 查询“男”教师及其所上的课程。 +select t.*,c.Cname from teacher t ,course c where t.tno= c.Tno and t.Tsex='男'; + +-- 查询最高分同学的Sno、Cno和Degree列。 +select * from score where Degree=(select max(Degree) from score); + +--  查询和“李军”同性别的所有同学的Sname. +select Sname from student where Ssex= (select Ssex from student where sname='李军'); + +--  查询和“李军”同性别并同班的同学Sname. +select Sname from student where Ssex= (select Ssex from student where sname='李军') and Class=(select Class from student where sname='李军'); + +-- 查询所有选修“计算机导论”课程的“男”同学的成绩表。 +select Ssex,Cname,Degree from student s,course c,score se where s.Sno=se.Sno and se.Cno=c.Cno and Cname='计算机导论' and Ssex='男'; +``` + diff --git "a/40 \346\226\207\346\231\272\345\213\207/\345\276\256\344\277\241\346\210\252\345\233\276_20230217224608.png" "b/40 \346\226\207\346\231\272\345\213\207/\345\276\256\344\277\241\346\210\252\345\233\276_20230217224608.png" deleted file mode 100644 index 78250d8b953acc1de3fcc8a1deed570abc52af8e..0000000000000000000000000000000000000000 Binary files "a/40 \346\226\207\346\231\272\345\213\207/\345\276\256\344\277\241\346\210\252\345\233\276_20230217224608.png" and /dev/null differ