From e13d5185f50a80f21d859c727add1c06c5799313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E6=99=BA=E7=A0=94?= <3058944672@qq.com> Date: Sun, 5 Mar 2023 12:04:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E2=80=98=E7=AC=AC=E4=BA=94=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\254\241\344\275\234\344\270\232.md.txt" | 217 ++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 "46 \350\203\241\346\231\272\347\240\224/20230305\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md.txt" diff --git "a/46 \350\203\241\346\231\272\347\240\224/20230305\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md.txt" "b/46 \350\203\241\346\231\272\347\240\224/20230305\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md.txt" new file mode 100644 index 0000000..340ebe9 --- /dev/null +++ "b/46 \350\203\241\346\231\272\347\240\224/20230305\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md.txt" @@ -0,0 +1,217 @@ +create database class charset utf8; +use class; +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`) +); +CREATE TABLE `course` +( + `Cno` varchar(20) NOT NULL, + `Cname` varchar(20) NOT NULL, + `Tno` varchar(20) NOT NULL, + PRIMARY KEY (`Cno`), + KEY `tno` (`Tno`), + CONSTRAINT `tno` FOREIGN KEY (`Tno`) REFERENCES `teacher` (`Tno`) +); +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`) +); +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`) +); +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, '李诚', '男', '1958-12-2', '副教授', '计算机系'), + (856, '张旭', '男', '1969-3-12', '讲师', '电子工程系'), + (825, '王萍', '女', '1972-5-5', '助教', '计算机系'), + (831, '刘冰', '女', '1977-8-14', '助教', '电子工程系'); +# 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息 +select * +from student + inner join score on student.Sno = score.Sno + inner join course on score.Cno = course.Cno; +# 2,查询没有学生的教师的所有信息 +select teacher.* +from teacher + left join course on teacher.Tno = course.Tno + left join score on course.Cno = score.Cno +where + Sno is null; +# ① 查询Score表中的最高分的学生学号和课程号。 +select s.Sno, Sname,cno +from student s + inner join score on s.Sno = score.Sno +where + Degree = (select max(Degree) from score); +# ② 查询所有学生的Sname、Cno和Degree列。 +select Sname, score.Cno, Degree +from student, + score +where student.Sno = score.Sno; +# ③ 查询所有学生的Sno、Cname和Degree列。 +select Sno, Cname, Degree +from course, + score +where course.Cno = score.Cno; +# ④ 查询所有学生的Sname、Cname和Degree列。 +select Sname, Cname, Degree +from student, + course, + score +where student.Sno = score.Sno + and course.Cno = score.Cno; +# ⑤ 查询“95033”班学生的平均分。 +select class,avg(Degree) from score s,student s2 where s.Sno = s2.Sno and class=95033; +# ⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select student.* +from student + inner join score on student.Sno = score.Sno +where Cno = '3-105' + and Degree > (student.Sno = 109); +# ⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select max(Degree) +from score +group by Cno; +select * +from score +where Degree not in (select max(Degree) from score group by Cno); +# ⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * +from score +where cno = '3-105' + and Degree > (sno = 109); +# ⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select Sno, Sname, Sbirthday +from student +where Sbirthday = (sno = 108); +# ⑩ 查询“张旭“教师任课的学生成绩。 +select Sno, Degree +from score s + inner join course c on c.cno = s.cno + inner join teacher t on c.Tno = t.Tno +where tname = '张旭'; +# 11 查询选修某课程的同学人数多于5人的教师姓名。 +select tname, count(c.Tno) +from score s + inner join course c on s.Cno = c.Cno + inner join teacher t on c.Tno = t.Tno +group by tname +having count(c.tno) > 5; +# 12 查询出“计算机系“教师所教课程的成绩表。 +select Depart, tname, sno, s.Cno, Degree +from teacher t + inner join course c on t.Tno = c.Tno + inner join score s on c.Cno = s.Cno +where Depart = '计算机系'; +# 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select Tname, Prof, Depart +from teacher +where Depart = '计算机系' + or Depart = '电子工程系'; +# 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select cno, st.Sno, degree +from score s + inner join student st on s.Sno = st.Sno +where cno = '3-105' + and Degree > (cno = '3-245') +order by degree desc +; +# 15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select s.Cno, Sno, Degree +from score s + inner join course c on s.Cno = c.Cno +where c.Cno = '3-105' + and Degree > (c.Cno = '3-245'); +# 16 查询成绩比该课程平均成绩低的同学的成绩表。 +select avg(Degree) +from score +group by Cno; +select * +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 t + left join course c on t.Tno = c.Tno + left join score s on s.Cno = c.Cno +where Sno is null; +# 19 查询“男”教师及其所上的课程。 +select c.* +from course c + inner join teacher t on c.Tno = t.Tno +where Tsex = '男'; +# 20 查询最高分同学的Sno、Cno和Degree列。 +select Sno, Cno, max(Degree) +from score; +# 21 查询和“李军”同性别的所有同学的Sname. +select Ssex +from student +where Sname = '李军'; +select Sname +from student +where Ssex = (select Ssex from student where Sname = '李军'); +# 22 查询和“李军”同性别并同班的同学Sname. +select class +from student +where Sname = '李军'; +select Sname +from student +where Ssex = (select Ssex from student where Sname = '李军') +and + class not in (select class +from student +where Sname = '李军'); +# 23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 +select s.* from score s +inner join student s2 on s.Sno = s2.Sno +inner join course c on s.Cno = c.Cno +where + Cname = '计算机导论' +and + Ssex='男'; \ No newline at end of file -- Gitee From 9eaa88b137cefb49fa4c1f9d2e06c8fb2b6437af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E6=99=BA=E7=A0=94?= <3058944672@qq.com> Date: Tue, 7 Mar 2023 23:52:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\254\241\344\275\234\344\270\232.md.txt" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "46 \350\203\241\346\231\272\347\240\224/20230307\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md.txt" diff --git "a/46 \350\203\241\346\231\272\347\240\224/20230307\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md.txt" "b/46 \350\203\241\346\231\272\347\240\224/20230307\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md.txt" new file mode 100644 index 0000000..fde50ca --- /dev/null +++ "b/46 \350\203\241\346\231\272\347\240\224/20230307\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md.txt" @@ -0,0 +1,87 @@ +CREATE DATABASE oiio charset utf8; +use oiio; +CREATE TABLE Stuinfo( +stuNO VARCHAR(10) PRIMARY KEY, +stuName VARCHAR(10) NOT NULL, +stuSex VARCHAR(10) NOT NULL, +stuAge int NOT NULL, +stuAddress VARCHAR(20) NOT NULL, +stuSeat int NOT NULL +); +CREATE TABLE stuExam( +examNO int NOT NULL, +stuNO VARCHAR(10) PRIMARY KEY, +writtenExam int NOT NULL, +labExam int NOT NULL +); +CREATE TABLE stuMarks( +examNO int NOT NULL, +stuD VARCHAR(10), +sxore int NOT NULL +); +INSERT INTO Stuinfo VALUES('s2501','张秋利','男',20,'美国硅谷',1); +INSERT INTO Stuinfo VALUES('s2502','李斯文','女',18,'湖北武汉',2); +INSERT INTO Stuinfo VALUES('s2503','马文才','男',18,'湖北长沙',3); +INSERT INTO Stuinfo VALUES('s2504','欧阳俊雄','女',21,'湖北武汉',4); +INSERT INTO Stuinfo VALUES('s2505','梅超风','男',20,'湖北武汉',5); +INSERT INTO Stuinfo VALUES('s2506','陈旋风','男',19,'美国硅谷',6); +INSERT INTO StuExam VALUES(1,'s2501',50,70); +INSERT INTO StuExam VALUES(2,'s2502',60,65); +INSERT INTO StuExam VALUES(3,'s2503',86,70); +INSERT INTO StuExam VALUES(4,'s2504',40,80); +INSERT INTO StuExam VALUES(5,'s2505',70,85); +INSERT INTO StuExam VALUES(6,'s2506',85,90); +INSERT INTO stuMarks VALUES(1,'s2501',88); +INSERT INTO stuMarks VALUES(1,'s2502',92); +INSERT INTO stuMarks VALUES(1,'s2503',53); +INSERT INTO stuMarks VALUES(2,'s2504',60); +INSERT INTO stuMarks VALUES(2,'s2505',99); +INSERT INTO stuMarks VALUES(3,'s2506',82); +-- 1.查询出年龄比班上平均年龄大的学生的信息 +-- SELECT avg(stuAge) FROM Stuinfo;平均年龄 +-- SELECT stuName stuAge FROM Stuinfo WHERE stuAge>(SELECT avg(stuAge) FROM Stuinfo); +SELECT * FROM stuinfo WHERE stuAge>(SELECT avg(stuAge) FROM stuinfo); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +SELECT stuNO,stuName,stuSex,MAX(sxore)FROM +stuinfo LEFT JOIN stumarks ON stuD = stuNo GROUP BY stuNo; +# 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +SELECT inf.stuNo,stuName,stuSex,(writtenExam+labexam)/2 FROM stuinfo inf LEFT JOIN stuexam ex on inf.stuNo = ex.stuNo GROUP BY stuNo; +# 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stusex = '男' and stuage >=20; +# 5.查询出年龄比所有男生年龄都大的女生的信息 +select max(stuage) from stuinfo where stusex = '男'; + +select * from stuinfo where stusex = '女' and stuage > (select max(stuage) from stuinfo where stusex = '男' +); +# 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +SELECT * FROM stuMarks RIGHT JOIN stuinfo ON stuD = stuNo WHERE sxore > 60 GROUP BY stuD; +# 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +SELECT * FROM stuMarks RIGHT JOIN stuinfo ON stuD = stuNo WHERE sxore is +not NULL GROUP BY stuD; +# 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +SELECT * FROM stuMarks RIGHT JOIN stuinfo ON stuD = stuNo WHERE sxore IS +null; +# 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +SELECT * FROM stuinfo LEFT JOIN stuMarks ON stuD = stuNo WHERE sxore >90; +# 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +SELECT * ,avg(sxore) FROM stuMarks GROUP BY stuD; + +SELECT * FROM stuinfo WHERE sxore = (SELECT avg(sxore) FROM stuMarks GROUP BY stuD;); + +SELECT *,AVG(sxore) FROM stuinfo,stuMarks WHERE stuD = stuNo GROUP BY stuD HAVING AVG(sxore)>80; +# 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select * from +(select *,max(sxore)from stuinfo +inner join stumarks on stuNo = stuD +group by stuD) maxsc +where > (select max(sxore) from stumarks where stuID='s2501'); +# 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select * from stumarks where sxore >(select * from stumarks inner join stuinfo on stumarks.stuD = stuNo where stuName = '张秋利'); +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo +where (stuage > (select max(stuAge) from stuinfo where stuSex = '男')) +and stuSex = '女'; +-- 14.查询出只是比某个男生年龄大的女生的信息 +select * from stuinfo +where (stuage > (select min(stuAge) from stuinfo where stuSex = '男')) +and stuSex = '女'; \ No newline at end of file -- Gitee