From 60d5f2ef10368ed5d74b81bba912df1aadf3ec23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E4=BC=9F=E5=B1=B1?= <2609838563@qq.com> Date: Sat, 4 Mar 2023 23:55:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E8=81=94=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\350\201\224\346\237\245\350\257\242 .md" | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 "39 \345\247\234\344\274\237\345\261\261/20230304 \345\205\263\350\201\224\346\237\245\350\257\242 .md" diff --git "a/39 \345\247\234\344\274\237\345\261\261/20230304 \345\205\263\350\201\224\346\237\245\350\257\242 .md" "b/39 \345\247\234\344\274\237\345\261\261/20230304 \345\205\263\350\201\224\346\237\245\350\257\242 .md" new file mode 100644 index 0000000..b15f5d6 --- /dev/null +++ "b/39 \345\247\234\344\274\237\345\261\261/20230304 \345\205\263\350\201\224\346\237\245\350\257\242 .md" @@ -0,0 +1,124 @@ +```mysql +create database class3 charset utf8; +use class3; +show tables; +create table Student( + Sno varchar(20) primary key , + Sname varchar(20) not null, + Ssex varchar(20), + Sbirthday datetime, + Class varchar(20) +); +select * from Student; +insert into student values + ('108','曾华','男','1997-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'); +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; +insert into Course values + ('3-105','计算机导论','825'), + ('3-245','操作系统','804'), + ('6-166','数字电路','856'), + ('9-888','高等数学','831'); +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; +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'); +create table Teacher( + Tno varchar(20) not null primary key , + Tname varchar(20) not null , + Tsex varchar(20) not null , + Tbirthday datetime, + Prof varchar(20), + Depart varchar(20) +); +insert into Teacher values + ('804','李诚','男','1958-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, score; +-- ③ 查询所有学生的Sno、Cname和Degree列。 +select Sno,Cname,Degree from course,score; +-- ④ 查询所有学生的Sname、Cname和Degree列。 +select Sname 姓名,Cname,Degree from course,score,student; +-- ⑤ 查询“95033”班学生的平均分。 +select AVG(Degree) from student,score WHERE Class=95033; +-- ⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +SELECT * from score where cno='3-105' and degree>76; +-- ⑦ 查询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='张旭'; +-- 11 查询选修某课程的同学人数多于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; +-- 12 查询出“计算机系“教师所教课程的成绩表。 +select tname ,prof from teacher where Depart='计算机系' +union +select tname ,prof from teacher where Depart='电子工程系'; +-- 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +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 ; +-- 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select * from score where Cno='3-105' and Degree > all (select Degree from score where Cno='3-245'); +-- 16 查询成绩比该课程平均成绩低的同学的成绩表。 +select avg(Degree) from score; +select Degree from score where Degree< (select avg(Degree) from score); + +-- 17 查询所有任课教师的Tname和Depart. +select Tname,Depart from teacher; +-- 18 查询所有未讲课的教师的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 ; + +-- 19 查询“男”教师及其所上的课程。 +select t.*,c.Cname from teacher t ,course c where t.tno= c.Tno and t.Tsex='男'; +-- 20 查询最高分同学的Sno、Cno和Degree列。 +select * from score where Degree=(select max(Degree) from score); +-- 21 查询和“李军”同性别的所有同学的Sname. +select Sname from student where Ssex= (select Ssex from student where sname='李军'); +-- 22 查询和“李军”同性别并同班的同学Sname. +select Sname from student where Ssex= (select Ssex from student where sname='李军') and Class=(select Class from student where sname='李军'); + +-- 23 查询所有选修“计算机导论”课程的“男”同学的成绩表 +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='男'; +``` + -- Gitee