From 1594a8b89bbd0cf6dbba876b1c68473f7c37b000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BC=BA?= <“3108497868@qq.com”> Date: Sun, 5 Mar 2023 11:44:14 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232.md" | 294 ++++++++++++++++++ 1 file changed, 294 insertions(+) create mode 100644 "45\347\216\213\345\274\272/\344\275\234\344\270\232.md" diff --git "a/45\347\216\213\345\274\272/\344\275\234\344\270\232.md" "b/45\347\216\213\345\274\272/\344\275\234\344\270\232.md" new file mode 100644 index 0000000..7081706 --- /dev/null +++ "b/45\347\216\213\345\274\272/\344\275\234\344\270\232.md" @@ -0,0 +1,294 @@ +```mysql +SHOW DATABASES; +CREATE DATABASE mxdx charset utf8; +USE mxdx; +CREATE DATABASE sb charset utf8; +USE sb; +CREATE TABLE student ( + sno VARCHAR ( 20 ) NOT NULL, + sname VARCHAR ( 20 ) NOT NULL, + ssex VARCHAR ( 20 ) NOT NULL, + sbirthday DATETIME, + class VARCHAR ( 20 ) +); +CREATE TABLE course ( cno VARCHAR ( 20 ) NOT NULL, cname VARCHAR ( 20 ) NOT NULL, tno VARCHAR ( 20 ) NOT NULL ); +ALTER TABLE course ADD FOREIGN KEY ( tno ) REFERENCES teacher ( tno ); +CREATE TABLE score ( sno VARCHAR ( 20 ) NOT NULL, cno VARCHAR ( 20 ) NOT NULL, degree DECIMAL ( 4, 1 ) ); +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 ) NOT NULL +); +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 ); +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( fegree ) + 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 From 797e9225826ba3a0e459b7f626eac7471f54cfdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BC=BA?= <“3108497868@qq.com”> Date: Tue, 7 Mar 2023 23:44:11 +0800 Subject: [PATCH 2/3] =?UTF-8?q?230307=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../23307\344\275\234\344\270\232.md" | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 "45\347\216\213\345\274\272/23307\344\275\234\344\270\232.md" diff --git "a/45\347\216\213\345\274\272/23307\344\275\234\344\270\232.md" "b/45\347\216\213\345\274\272/23307\344\275\234\344\270\232.md" new file mode 100644 index 0000000..bbcabcb --- /dev/null +++ "b/45\347\216\213\345\274\272/23307\344\275\234\344\270\232.md" @@ -0,0 +1,5 @@ +```mysql + +``` + + -- Gitee From 0548df081bfee25eda36efc6f2bd5f14172d9449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BC=BA?= <“3108497868@qq.com”> Date: Tue, 7 Mar 2023 23:48:42 +0800 Subject: [PATCH 3/3] =?UTF-8?q?:230307=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../23307\344\275\234\344\270\232.md" | 81 ++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git "a/45\347\216\213\345\274\272/23307\344\275\234\344\270\232.md" "b/45\347\216\213\345\274\272/23307\344\275\234\344\270\232.md" index bbcabcb..b9cd24d 100644 --- "a/45\347\216\213\345\274\272/23307\344\275\234\344\270\232.md" +++ "b/45\347\216\213\345\274\272/23307\344\275\234\344\270\232.md" @@ -1,5 +1,84 @@ ```mysql +create database sb charset utf8; +use sb -``` +create table stuinfo( +stuno varchar(20) primary key, +stuname varchar(20), +stusex varchar(10), +stuage int(10), +stuaddress varchar(20), +stuseat int(10) +); + +drop table stuinfo; +drop table stuexam; +drop table stumarks; + +create table stuexam( +examno int(10) primary key auto_increment, +stuno varchar(20), +writtenexam int(20), +labexam int(20) +); + +create table stumarks( +examno int(10) primary key auto_increment, +stuid varchar(20), +score int(10) +); + +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','马文才','男',,'湖南长沙',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), +@@ -56,8 +60,6 @@ insert into stumarks values +(5,'s2505',99), +(6,'s2506',82); + + + +-- 1.查询出年龄比班上平均年龄大的学生的信息 +select * from stuinfo where stuage>(select avg(stuage) from stuinfo); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select z.stuno,z.stuname,z.stusex,max(score) from stuinfo z inner join stumarks j on z.stuno = j.stuid group by stuno; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select z.stuno,z.stuname,z.stusex,((j.writtenexam + j.labexam)/2)考试平均分 from stuinfo z left join stuexam j on z.stuno = j.stuno; +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuage >= 20 and stusex = '男'; +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stusex = '女' and stuage > (select max(stuage) from stuinfo where stusex = '男'); +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select z.* from stuinfo z,(select stuid from stumarks group by stuid having min(score) >= 60) j where z.stuno = j.stuid; +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select z.* from stuinfo z,(select stuid from stumarks group by stuid) j where a.stuno =j.stuid; +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select * from stuinfo where stuno not in (select stuid from stumarks group by stuid); +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select z.* from stuinfo z,(select stuid from stumarks group by stuid having max(score) > 90) j where z.stuno = j.stuid; +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select z.* from stuinfo z,(select stuid from stumarks group by stuid having avg(score) > 80) j where z.stuno = j.stuid; +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select z.* from stuinfo z,(select stuid from stumarks where stuid != '2501' group by stuid having max(score) > (select max(score) from stumarks where stuid != '2501')) j where z.stuno = j.stuid; +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +@@ -85,4 +87,8 @@ select * from stuinfo z, (select stuid from stumarks where stuid != '2501'group +-- 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 = '男'); +\ No newline at end of file +select * from stuinfo where stusex = '女' and stuage > (select min(stuage) from stuinfo where stusex = '男'); +``` -- Gitee