diff --git "a/02 \346\235\216\346\214\257\345\215\216/20230228 MySQL\350\201\224\350\241\250\346\237\245\350\257\242\344\275\234\344\270\232.md.md" "b/02 \346\235\216\346\214\257\345\215\216/20230228 MySQL\350\201\224\350\241\250\346\237\245\350\257\242\344\275\234\344\270\232.md.md" new file mode 100644 index 0000000000000000000000000000000000000000..cefe9fe4bfcda8c6909b3c813ece9d54f4e04218 --- /dev/null +++ "b/02 \346\235\216\346\214\257\345\215\216/20230228 MySQL\350\201\224\350\241\250\346\237\245\350\257\242\344\275\234\344\270\232.md.md" @@ -0,0 +1,136 @@ +```mysql +create table student( +Sno varchar (20) not null COMMENT '学号' PRIMARY KEY, +Sname varchar (20)not null COMMENT '学生姓名', +Ssex varchar (20)not null COMMENT '学生性别', +Sbirthday datetime not null COMMENT '学生出生年月', +Class varchar (20) not null COMMENT '学生所在班级' +); +create table course( +Cno varchar (20) not null COMMENT '课程号' PRIMARY KEY, +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 '学号',FOREIGN KEY (Sno) REFERENCES student(Sno), +Cno varchar (20) not null COMMENT '课程号',FOREIGN KEY (Cno) REFERENCES course(Cno), +Degree Decimal(4,1) DEFAULT null COMMENT'成绩', +primary key(Sno,Cno) +); +create table teacher( +Tno varchar (20) not null COMMENT'教工编号' PRIMARY KEY, +Tname varchar (20) not null COMMENT'教工姓名', +Tsex varchar (20) not null COMMENT'教工性别', +Tbirthday datetime DEFAULT null COMMENT'教工出生年月', +Prof varchar (20) DEFAULT null COMMENT'职称', +Depart varchar (20) not null COMMENT'教工所在部门' +); +INSERT INTO `student` VALUES ('108', '曾华', '男', '1977-09-01 00:00:00', '95033'); +INSERT INTO `student` VALUES ('105', '匡明', '男', '1975-10-02 00:00:00', '95031'); +INSERT INTO `student` VALUES ('107', '王丽', '女', '1976-01-23 00:00:00', '95033'); +INSERT INTO `student` VALUES ('101', '李军', '男', '1976-02-20 00:00:00', '95033'); +INSERT INTO `student` VALUES ('109', '王芳', '女', '1975-02-10 00:00:00', '95031'); +INSERT INTO `student` VALUES ('103', '陆君', '男', '1974-06-03 00:00:00', '95031'); + +INSERT INTO `teacher` VALUES ('804', '李诚', '男', '1958-12-02 00:00:00', '副教授', '计算机系'); +INSERT INTO `teacher` VALUES ('856', '张旭', '男', '1969-03-12 00:00:00', '讲师', '电子工程系'); +INSERT INTO `teacher` VALUES ('825', '王萍', '女', '1972-05-05 00:00:00', '助教', '计算机系'); +INSERT INTO `teacher` VALUES ('831', '刘冰', '女', '1977-08-14 00:00:00', '助教', '电子工程系'); + +INSERT INTO `course` VALUES ('3-105', '计算机导论', '825'); +INSERT INTO `course` VALUES ('3-245', '操作系统', '804'); +INSERT INTO `course` VALUES ('6-166', '数字电路', '856'); +INSERT INTO `course` VALUES ('9-888', '高等数学', '831'); + +INSERT INTO `score` VALUES ('103', '3-245', 86.0); +INSERT INTO `score` VALUES ('109', '3-245', 68.0); +INSERT INTO `score` VALUES ('103', '3-105', 92.0); +INSERT INTO `score` VALUES ('105', '3-105', 88.0); +INSERT INTO `score` VALUES ('109', '3-105', 76.0); +INSERT INTO `score` VALUES ('101', '3-105', 64.0); +INSERT INTO `score` VALUES ('107', '3-105', 91.0); +INSERT INTO `score` VALUES ('108', '3-105', 92.0); +INSERT INTO `score` VALUES ('101', '6-166', 85.0); +INSERT INTO `score` VALUES ('107', '6-166', 79.0); +INSERT INTO `score` VALUES ('108', '6-166', 81.0); +INSERT INTO `score` VALUES ('103', '2-165', 88.0); +``` + +```mysql +SELECT + stu.*,c.* +FROM + student stu + LEFT JOIN score s ON stu.sno = s.sno + left join course c on s.cno = c.cno; + + SELECT + t.* + FROM + teacher t + LEFT JOIN course c ON c.tno = t.tno + left join score s on s.cno=c.cno + WHERE sno is null; +SELECT +Sno,Cno,Degree +FROM +score WHERE Degree=(SELECT MAX(Degree) FROM score); + +SELECT +Sname,Cno,Degree +FROM +student stu LEFT JOIN score sco on stu.Sno=sco.Sno; + +SELECT +Sno,Cname,Degree +FROM +score s LEFT JOIN course c on s.Cno=c.Cno; + +SELECT +Sname,Cname,Degree +FROM +student LEFT JOIN score on student.Sno=score.Sno +LEFT JOIN course on score.Cno=course.Cno; + +SELECT +class 班级, +ROUND(AVG(Degree),2) 平均分 FROM +student LEFT JOIN score on student.Sno=score.Sno WHERE class='95033'; + +select a.* from score a,score b where a.cno='3-105' and a.cno=b.cno and b.sno=109 and a.degree>b.degree; + +select * from score a where sno in (select sno from score group by sno having count(*)>1 -- 选学多门课程的同学 + ) and degree=(select max(degree) from score b where a.cno=b.cno ); + +select * from Score where cno='3-105' and Degree>(select degree from Score where cno='3-105' and Sno='109'); + +select Sno,Sname,Sbirthday from Student where YEAR(Sbirthday)=(select YEAR(sbirthday)from Student where Sno='108'); + +Select Degree from score where cno=( Select cno from course where tno=(Select tno from teacher where tname='张旭')); + +Select tname from teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having COUNT(*)>5)); + +select * from score where Cno in(select cno from Course where Tno in(select tno from Teacher where Depart='计算机系')); + +select Tname,Prof from teacher where prof not in (select prof from teacher where depart = '计算机系' and prof in (select prof from teacher where depart ='电子工程系')); + +select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select MAX(degree)from Score where Cno='3-245') order by Degree desc; + +select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select MAX(degree)from Score where Cno='3-245'); + +Select degree from score a where degree<(select avg(degree) from score b where a.cno=b.cno); + +Select tname,depart from teacher where tno in (select tno from course where cno in (select cno from score group by cno)); +Select tname,depart from teacher where tno in (select tno from course where cno in (select distinct cno from score)); +Select tname,depart from teacher where tno not in (select tno from course where cno in (select distinct cno from score)); +Select tname,cname from teacher t join course c on t.tno=c.tno and t.tsex='男'; + +select Sno,Cno,Degree from Score where Degree in(select MAX(Degree) from Score); + +Select sname from student where ssex=(select ssex from student where sname='李军'); + +Select sname from student where ssex=(select ssex from student where sname='李军') and Class=(select class from student where sname='李军'); + + select degree from Score where Sno in(select Sno from Student where Ssex='男') and Cno in (select Cno from Course where cname='计算机导论'); +``` + diff --git "a/02 \346\235\216\346\214\257\345\215\216/20230307 MySQL\345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md.sql" "b/02 \346\235\216\346\214\257\345\215\216/20230307 MySQL\345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ef8f6ec257befeb523cb6a03fbc65a4140283b1c --- /dev/null +++ "b/02 \346\235\216\346\214\257\345\215\216/20230307 MySQL\345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md.sql" @@ -0,0 +1,100 @@ +#1.查询出年龄比班上平均年龄大的学生的信息 + +```mysql +SELECT * FROM stuinfo WHERE stuAge>(SELECT avg(stuAge) FROM stuinfo); +``` + +#2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) + +```mysql +SELECT stuNo,stuName,stuSex,MAX(score) +FROM stuinfo a RIGHT JOIN stumarks b on a.stuNo=b.stuID GROUP BY stuID; +``` + +#3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) + +```mysql +SELECT a.stuNo,stuName,stuSex,(writtenExam+labExam)/2 as AVG +FROM stuinfo a LEFT JOIN stuexam b on a.stuNo=b.stuNo; +``` + +#4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +#普通查询 + +```mysql +SELECT * FROM stuinfo WHERE stuAge>=20 and stuSex='男'; +``` + +#子查询 +; + +```mysql +SELECT * FROM stuinfo WHERE stuAge>=20; +SELECT * FROM (SELECT * FROM stuinfo WHERE stuSex='男' ) a WHERE stuAge>=20; +``` + +#5.查询出年龄比所有男生年龄都大的女生的信息 + +```mysql +SELECT * FROM stuinfo WHERE stuSex='女' HAVING stuAge>(SELECT MAX(stuAge) FROM stuinfo WHERE stuSex='男'); +``` + +#6.查询出所有选修课程都及格的学生的信息 (stuMarks) + +```mysql +SELECT a.* FROM stuinfo a JOIN stumarks b on a.stuNo=b.stuID WHERE stuNo not in(SELECT stuNo FROM stuinfo JOIN stumarks on stuinfo.stuNo=stumarks.stuID WHERE score<60) GROUP BY stuName; +``` + +#7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +```mysql +SELECT a.* FROM stuinfo a JOIN stumarks b on a.stuNo=b.stuID WHERE stuNo not in(SELECT stuNo FROM stuinfo left JOIN stumarks on stuinfo.stuNo=stumarks.stuID WHERE score is null) GROUP BY stuName; + +SELECT a.* FROM stuinfo a LEFT JOIN stumarks b on a.stuNo=b.stuID WHERE stuNo in ('s2501','s2502','s2503') GROUP BY stuNo; +``` + +#8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +```mysql +SELECT a.* FROM stuinfo a LEFT JOIN stumarks b on a.stuNo=b.stuID WHERE stuNo in ('s2504','s2505','s2506') GROUP BY stuNo; + + +SELECT a.* FROM stuinfo a LEFT JOIN stumarks b on a.stuNo=b.stuID WHERE score is null; +``` + +#9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) + +```mysql +SELECT a.* FROM stuinfo a RIGHT JOIN stumarks b on a.stuNo=b.stuID WHERE score in(SELECT score FROM stumarks WHERE score>90); +``` + +#10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) + +```mysql +select stuNO,stuname,stusex,stuage,stuaddress,stuseat,avg(score) pingjun from stuinfo a left join stumarks b on a.stuNo=b.stuId group by stuNO having pingjun>80; +``` + +#11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) + +```mysql +select a.* from stuinfo a join stuMarks b on a.stuNo=b.stuID WHERE score>(select MAX(score) from stuinfo a join stuMarks b on a.stuNo=b.stuID WHERE stuName='张秋利'); +``` + +#12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) + +```mysql +select a.* from stuinfo a join stuMarks b on a.stuNo=b.stuID WHERE score>all(select score from stuinfo a join stuMarks b on a.stuNo=b.stuID WHERE stuName='张秋利'); +``` + +#13.查询班上比所有男生年龄都要大的女生的信息 + +```mysql +SELECT * FROM stuinfo WHERE stuSex='女' HAVING stuAge>(SELECT MAX(stuAge) FROM stuinfo WHERE stuSex='男'); +``` + +#14.查询出只要比某个男生年龄大的女生的信息 + +```mysql +SELECT * FROM stuinfo WHERE stuSex='女' HAVING stuAge>(SELECT MAX(stuAge) FROM stuinfo WHERE stuSex='男'); +``` +