From a0dd3d50a302174f20091a1c33ec8027b27f5647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=98=E5=B0=8Fqiang?= <3249994165@qq.com> Date: Sun, 5 Mar 2023 11:35:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=81=9A=E5=90=88=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=BF=9E=E8=A1=A8=E6=9F=A5=E8=AF=A2=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...24\350\241\250\346\237\245\350\257\242.md" | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 "41 \347\224\230\345\260\217\345\274\272/3.5\350\201\232\345\220\210\345\207\275\346\225\260\350\201\224\350\241\250\346\237\245\350\257\242.md" diff --git "a/41 \347\224\230\345\260\217\345\274\272/3.5\350\201\232\345\220\210\345\207\275\346\225\260\350\201\224\350\241\250\346\237\245\350\257\242.md" "b/41 \347\224\230\345\260\217\345\274\272/3.5\350\201\232\345\220\210\345\207\275\346\225\260\350\201\224\350\241\250\346\237\245\350\257\242.md" new file mode 100644 index 0000000..c9d7b8f --- /dev/null +++ "b/41 \347\224\230\345\260\217\345\274\272/3.5\350\201\232\345\220\210\345\207\275\346\225\260\350\201\224\350\241\250\346\237\245\350\257\242.md" @@ -0,0 +1,110 @@ +```mysql +use class; +CREATE TABLE `course` ( + `Cno` varchar(255) NOT NULL, + `Cname` varchar(255) NOT NULL, + `Tno` varchar(255) NOT NULL, + PRIMARY KEY (`Cno`), + KEY `Tno` (`Tno`), + CONSTRAINT `Tno` FOREIGN KEY (`Tno`) REFERENCES `teacher` (`Tno`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE `score` ( + `Sno` varchar(20) NOT NULL, + `Cno` varchar(20) NOT NULL, + `Degree` decimal(4,1) DEFAULT NULL, + PRIMARY KEY (`Sno`,`Cno`), + KEY `Cno` (`Cno`), + CONSTRAINT `Cno` FOREIGN KEY (`Cno`) REFERENCES `course` (`Cno`), + CONSTRAINT `Sno` FOREIGN KEY (`Sno`) REFERENCES `student` (`Sno`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE `student` ( + `Sno` varchar(20) NOT NULL, + `Sname` varchar(20) NOT NULL, + `Ssex` varchar(20) NOT NULL, + `Sbirthday` datetime DEFAULT NULL, + `Class` varchar(20) DEFAULT NULL, + PRIMARY KEY (`Sno`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE `teacher` ( + `Tno` varchar(20) NOT NULL, + `Tname` varchar(20) NOT NULL, + `Tsex` varchar(20) NOT NULL, + `Tbirthday` datetime DEFAULT NULL, + `Prof` varchar(20) DEFAULT NULL, + `Depart` varchar(20) NOT NULL, + PRIMARY KEY (`Tno`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +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',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 values (804,'李诚','男','1985-12-2','副教授','计算机系'), +(856,'张旭','男','1969-3-12','讲师','电子工程系'), +(825,'王萍','女','1972-5-5','助教','计算机系'), +(831,'刘冰','女','1977-8-14','助教','电子工程系'); +-- 4. 查询 +-- ① 查询Score表中的最高分的学生学号和课程号。 +select MAX(Degree) `Sno`, `Cno` from score; +-- ② 查询所有学生的Sname、Cno和Degree列。 +select Sname 姓名,Cno,Degree from student left JOIN score on student.sno=score.sno; +-- ③ 查询所有学生的Sno、Cname和Degree列。 +select Sno,Cname,Degree from course a inner join score b on a.cno=b.cno; +-- ④ 查询所有学生的Sname、Cname和Degree列。 +select * from course a +inner JOIN score b on a.cno=b.cno +inner join student c on c.sno=b.sno; +-- ⑤ 查询“95033”班学生的平均分。 +select AVG(Degree) from student,score WHERE Class=95033; +-- ⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +SELECT * from score where cno='3-105' and degree>(SELECT degree from score where cno='3-105' and Sno=109); +-- ⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select (MAX(degree) is null) from score ; +-- ⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +SELECT * from score where cno='3-105' and degree>(SELECT degree from score where cno='3-105' and Sno=109); +-- ⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select * from student where Sbirthday= '1977-9-1'; +-- ⑩ 查询“张旭“教师任课的学生成绩。 +select degree from teacher,score where tname='张旭'; +-- 11 查询选修某课程的同学人数多于5人的教师姓名。 +select tname,b.cno from teacher a inner join course c on a.tno=c.tno inner join score b on c.cno=b.cno group by c.cno having count(b.cno)>5; +-- 12 查询出“计算机系“教师所教课程的成绩表。 +select tname,depart,prof,c.cno,cname from teacher b inner join course c on b.tno=c.tno inner join score a on c.cno=a.cno where Depart='计算机系'; +-- 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select tname,prof,Depart from teacher; +-- 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select cno, Sno, degree from score where cno = '3-105'and Degree > (cno = '3-245') ORDER BY degree desc; +-- 15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select cno, Sno, degree from score where cno = '3-105'and Degree > (cno = '3-245'); +-- 16 查询成绩比该课程平均成绩低的同学的成绩表。 +select AVG(Degree) from score a inner join course b on a.cno=b.cno where cname='计算机导论'or cname='操作系统'or cname='高等数学'or cname='数字电路' and Degree<(select avg(Degree) from score); +-- 17 查询所有任课教师的Tname和Depart. +select tname,depart from teacher; +-- 18 查询所有未讲课的教师的Tname和Depart. +select tname,depart from teacher a left join course b on a.tno=b.tno left join score c on b.cno=c.cno where c.sno is null; +-- 19 查询“男”教师及其所上的课程。 +select * from course a left join teacher b on a.tno=b.tno where tsex='男'; +-- 20 查询最高分同学的Sno、Cno和Degree列。 +select sno,cno,degree from score where degree=(select max(Degree)from score) ; +-- 21 查询和“李军”同性别的所有同学的Sname. +select sname from student where ssex=(select ssex from student where sno=101); +-- 22 查询和“李军”同性别并同班的同学Sname. +select sname from student where ssex=(select ssex from student where sno=101) and class=95033; +-- 23 查询所有选修“计算机导论”课程的“男”同学的成绩表 +select sname,ssex,cname,Degree from course a inner join score b on a.cno=b.cno inner join student c on b.sno=c.sno where ssex='男' and cname='计算机导论'; +-- +``` + -- Gitee From 2eed7fe9517fb9ad5c3361934aed59a09a61f92f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=98=E5=B0=8Fqiang?= <3249994165@qq.com> Date: Tue, 7 Mar 2023 22:49:22 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AD=90=E8=A1=A8=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\350\257\242\347\273\203\344\271\240.md" | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 "41 \347\224\230\345\260\217\345\274\272/3.7\345\255\220\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240.md" diff --git "a/41 \347\224\230\345\260\217\345\274\272/3.7\345\255\220\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240.md" "b/41 \347\224\230\345\260\217\345\274\272/3.7\345\255\220\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240.md" new file mode 100644 index 0000000..fd141b2 --- /dev/null +++ "b/41 \347\224\230\345\260\217\345\274\272/3.7\345\255\220\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240.md" @@ -0,0 +1,89 @@ +```mysql +CREATE table stu( + stuNO VARCHAR(10) primary key, + stuName VARCHAR(10), + stuSex VARCHAR(10), + stuAge VARCHAR(10), + stuAddress VARCHAR(20), + stuSeat INT +); +CREATE table Exam( + examNo int primary key auto_increment, + stuNO VARCHAR(10), + writtenExam int, + labExam INT +); +CREATE table EXAMM( + examNo int primary key auto_increment, + stuID VARCHAR(10), + score INT +); +alter table EXAM add foreign key (stuNO) references stu(stuNO); +alter table EXAMM add foreign key (examNo) references EXAM(examNo); + + +INSERT into stu VALUES + ('s2501','张秋利','男',20,'美国硅谷',1), + ('s2502','李斯文','女',18,'湖北武汉',2), + ('s2503','马文才','男',18,'湖南长沙',3), + ('s2504','欧阳俊雄','女',21,'湖北武汉',4), + ('s2505','梅超风','男',20,'湖北武汉',5), + ('s2506','陈旋风','男',19,'美国硅谷',6); + +INSERT INTO Exam 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 EXAMM VALUES + (1,'s2501',88), + (2,'s2501',92), + (3,'s2501',53), + (4,'s2502',60), + (5,'s2502',99), + (6,'s2503',82); + +-- 在如图的数据表上完成以下题目 +-- 1.查询出年龄比班上平均年龄大的学生的信息 +select avg(stuage) from stu; +select * from stu where stuage>(select avg(stuage) from stu); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select stuno,stuname,stusex,max(score) from stu left join examm on stu.stuNO=examm.stuID group by stuNo; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select stuno,stuname,stusex,avg(score) from stu left join examm on stu.stuNO=examm.stuID group by stuno; +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stu where stusex='男' and stuage>=20; +select * from stu where stusex in(select stusex from stu where stusex='男') and stuage in(select stuage from stu where stuage>=20); +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stu where stuage>(select max(stuage) from stu where stusex='男') and stusex='女'; +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select * from examm where score>60; +select min(score) from examm; +select * from examm right join stu on examm.stuid=stu.stuno group by stuid having min(score)>=60 ; +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select *from stu right join examm on stu.stuno=examm.stuid group by stuid; +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select stu.* from stu left join examm on stu.stuno=examm.stuid where score is null; +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select * from stu left join examm on stu.stuno=examm.stuid where score>90; +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select * from stu left join examm on stu.stuno=examm.stuid where score in(select score from examm where score>80 ); +select avg(score) from examm ; +select * from stu left join examm on stu.stuno=examm.stuid where score in(select avg(score) num from examm group by stuid having num>80); +select * from stu a,(select stuid from examm group by stuid having avg(score)>80) b where a.stuNO=b.stuID; +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +SELECT score from examm where score> +select * from stu a left join examm b on a.stuno=b.stuid where + score>(select max(score) from examm where stuid='s2501') ; +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +SELECT * from examm left join stu on examm.stuid=stu.stuno where score>(select min(score) from examm,stu where stuname='张秋利') and stuname !='张秋利' ; +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stu where stuage>(select max(stuage) from stu where stusex='男') and stusex='女'; +-- 14.查询出只是比某个男生年龄大的女生的信息 +select * from stu where stuage>(select min(stuage) from stu where stusex='男') and stusex='女'; +``` + + -- Gitee