From ddae5a5f24f6b2959107d7d76f2216fd34f4dbef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=81=A9=E6=B0=91?= <3256972080@qq.com> Date: Sun, 5 Mar 2023 12:21:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E4=BA=94=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 --- ...54\344\272\224\346\254\241\350\257\276.md" | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 "30 \350\256\270\346\201\251\346\260\221/\347\254\254\344\272\224\346\254\241\350\257\276.md" diff --git "a/30 \350\256\270\346\201\251\346\260\221/\347\254\254\344\272\224\346\254\241\350\257\276.md" "b/30 \350\256\270\346\201\251\346\260\221/\347\254\254\344\272\224\346\254\241\350\257\276.md" new file mode 100644 index 0000000..ba90fa5 --- /dev/null +++ "b/30 \350\256\270\346\201\251\346\260\221/\347\254\254\344\272\224\346\254\241\350\257\276.md" @@ -0,0 +1,118 @@ +``` +CREATE DATABASE yyy CHARSET utf8 ; +USE job ; +CREATE TABLE Student( +Sno VARCHAR(20) COMMENT'学号' PRIMARY KEY, +Sname VARCHAR(20) COMMENT'学生姓名' not null, +Ssex VARCHAR(20) COMMENT'学生性别' not null, +Sbirthday datetime COMMENT'学生出生年月' null, +Class VARCHAR(20) COMMENT'学生所在班级' null +); +CREATE table Teacher( +Tno VARCHAR(20) COMMENT'教工编号' PRIMARY KEY, +Tname VARCHAR(20) COMMENT'教工姓名' not null, +Tsex VARCHAR(20) COMMENT'教工性别' not null, +Tbirthday datetime COMMENT'教工出生年月' null, +Prof VARCHAR(20) COMMENT'职称' null, +Depart VARCHAR(20) COMMENT'教工所在部门' not null +); +CREATE table Course( +Cno VARCHAR(20) COMMENT'课程号' PRIMARY KEY, +Cname VARCHAR(20) COMMENT'课程名称' not null, +Tno VARCHAR(20) COMMENT'教工编号' not null, +FOREIGN key (Tno) REFERENCES Teacher(Tno) +); +CREATE table Score( +Sno VARCHAR(20) COMMENT '学号' not null, +Cno VARCHAR(20) COMMENT'课程号' not null, +Degree DECIMAL(4,1) COMMENT'成绩'NULL, +FOREIGN key (Sno) REFERENCES Student(Sno), +FOREIGN key (Cno) REFERENCES Course(Cno) +); + +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 Teacher VALUES +(804, '李诚',' 男','1958-12-2','副教授',' 计算机系'), +(856, '张旭', '男 ','1969-3-12','讲师','电子工程系'), +(825, '王萍', '女 ','1972-5-5', '助教','计算机系'), +(831, '刘冰', '女','1977-8-14', '助教','电子工程系'); + +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); +-- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ +SELECT S.*,C.Cno,C.Cname from Student S left JOIN Score Sc on S.Sno =Sc.Sno + LEFT JOIN Course C ON Sc.Cno = C.Cno ; +-- 2,查询没有学生的教师的所有信息 +SELECT T.* FROMTeacher T LEFT JOIN Course C ON C.tno = T.tno left join Score Sc on Sc.cno=C.cno WHERE Sno is null; +-- 4. 查询 +-- ① 查询Score表中的最高分的学生学号和课程号。 +SELECT S.Sno,C.Cno,Sc.Degree FROM Student S LEFT JOIN Score Sc ON S.Sno =Sc.Sno +LEFT JOIN Course C ON Sc.Cno = C.Cno WHERE Sc.Degree = (SELECT MAX(Degree) from Score); +-- ② 查询所有学生的Sname、Cno和Degree列。 +SELECT S.Sname,C.Cno,Sc.Degree from Student S left JOIN Score Sc on S.Sno =Sc.Sno + LEFT JOIN Course C ON Sc.Cno = C.Cno ; +-- ③ 查询所有学生的Sno、Cname和Degree列。 +SELECT S.Sno,C.Cname,Sc.Degree from Student S left JOIN Score Sc on S.Sno =Sc.Sno + LEFT JOIN Course C ON Sc.Cno = C.Cno ; +-- ④ 查询所有学生的Sname、Cname和Degree列。 +SELECT S.Sname,C.Cname,Sc.Degree from Student S left JOIN Score Sc on S.Sno =Sc.Sno + LEFT JOIN Course C ON Sc.Cno = C.Cno ; +-- ⑤ 查询“95033”班学生的平均分。 +SELECT ROUND(avg(Sc.Degree),2) FROM Student S INNER JOIN Score Sc ON S.Sno = Sc.Sno WHERE class=95033 ; +-- ⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select Sc.* from Score Sc,Score S where Sc.Cno='3-105' and Sc.Cno=S.Cno and S.Sno=109 and Sc.Degree>S.Degree; +-- ⑦ 查询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 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='张旭')); +-- 11 查询选修某课程的同学人数多于5人的教师姓名。 +Select tname from teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having COUNT(*)>5)); +-- 12 查询出“计算机系“教师所教课程的成绩表。 +select * from score where Cno in(select cno from Course where Tno in(select tno from Teacher where Depart='计算机系')); +-- 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select Tname,Prof from teacher where prof not in (select prof from teacher where depart = '计算机系' and prof in (select prof from teacher where depart ='电子工程系')); +-- 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +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; +-- 15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +SELECT Cno,Sno,Degree FROM Score WHERE Cno='3-105' and Degree>(SELECT MAX(Degree)FROM Score WHERE Cno='3-245'); +-- 16 查询成绩比该课程平均成绩低的同学的成绩表。 +-- 17 查询所有任课教师的Tname和Depart. +SELECT Tname,Depart from Teacher WHERE Tno IN (SELECT Tno from Course WHERE Cno IN (select Cno from Score group by Cno)); +-- 18 查询所有未讲课的教师的Tname和Depart. +SELECT Tname,Depart from Teacher where Tno not in (select Tno from Course where Cno in (select distinct Cno from Score)); +-- 19 查询“男”教师及其所上的课程。 + SELECT Tname,Cname from Teacher T join Course C on T.Tno=C.Tno and T.Tsex='男'; +-- 20 查询最高分同学的Sno、Cno和Degree列。 + SELECT Sno,Cno,Degree from Score where Degree in( SELECT MAX(Degree) from Score); +-- 21 查询和“李军”同性别的所有同学的Sname. +SELECT Sname,Ssex 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 Degree from Score where Sno in(select Sno from Student where Ssex='男') and Cno in (select Cno from Course where Cname='计算机导论'); +``` \ No newline at end of file -- Gitee From e302e86bc48c9c9a1534f7f11d7e2149424733c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=81=A9=E6=B0=91?= <3256972080@qq.com> Date: Tue, 7 Mar 2023 22:56: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 --- ...54\345\205\255\346\254\241\350\257\276.md" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 "30 \350\256\270\346\201\251\346\260\221/\347\254\254\345\205\255\346\254\241\350\257\276.md" diff --git "a/30 \350\256\270\346\201\251\346\260\221/\347\254\254\345\205\255\346\254\241\350\257\276.md" "b/30 \350\256\270\346\201\251\346\260\221/\347\254\254\345\205\255\346\254\241\350\257\276.md" new file mode 100644 index 0000000..7bbd761 --- /dev/null +++ "b/30 \350\256\270\346\201\251\346\260\221/\347\254\254\345\205\255\346\254\241\350\257\276.md" @@ -0,0 +1,70 @@ +CREATE TABLE `aaa` ( + `stuno` varchar(5) COLLATE utf8mb4_0900_as_ci NOT NULL PRIMARY KEY, + `stuname` varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_as_ci NOT NULL, + `stusex` char(1) COLLATE utf8mb4_0900_as_ci NOT NULL, + `stuage` int NOT NULL, + `stuaddress` varchar(4) COLLATE utf8mb4_0900_as_ci NOT NULL, + `stuseat` int NOT NULL +) ; +CREATE TABLE `bbb` ( + `examno` int NOT NULL, + `stuno` varchar(5) NOT NULL, + `writtenexam` int NOT NULL, + `labexam` int NOT NULL, + PRIMARY KEY (`examno`), + FOREIGN KEY (stuno) REFERENCES aaa(stuno) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_as_ci; +CREATE TABLE `ccc` ( + `examno` int NOT NULL, + `stuid` varchar(5) COLLATE utf8mb4_0900_as_ci NOT NULL, + `score` int NOT NULL, + PRIMARY KEY (`examno`), + FOREIGN KEY (stuid) REFERENCES aaa(stuno) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_as_ci; +INSERT INTO `aaa` (`stuno`, `stuname`, `stusex`, `stuage`, `stuaddress`, `stuseat`) VALUES ('s2501', '张秋利', '男', 20, '美国硅谷', 1); +INSERT INTO `aaa` (`stuno`, `stuname`, `stusex`, `stuage`, `stuaddress`, `stuseat`) VALUES ('s2502', '李斯文', '女', 18, '湖北武汉', 2); +INSERT INTO `aaa` (`stuno`, `stuname`, `stusex`, `stuage`, `stuaddress`, `stuseat`) VALUES ('s2503', '马文才', '男', 22, '湖南长沙', 3); +INSERT INTO `aaa` (`stuno`, `stuname`, `stusex`, `stuage`, `stuaddress`, `stuseat`) VALUES ('s2504', '欧阳俊熊', '女', 21, '湖北武汉', 4); +INSERT INTO `aaa` (`stuno`, `stuname`, `stusex`, `stuage`, `stuaddress`, `stuseat`) VALUES ('s2505', '梅超风', '男', 20, '湖北武汉', 5); +INSERT INTO `aaa` (`stuno`, `stuname`, `stusex`, `stuage`, `stuaddress`, `stuseat`) VALUES ('s2506', '陈旋风', '男', 19, '美国硅谷', 6); +INSERT INTO `bbb` (`examno`, `stuno`, `writtenexam`, `labexam`) VALUES (1, 's2501', 50, 70); +INSERT INTO `bbb` (`examno`, `stuno`, `writtenexam`, `labexam`) VALUES (2, 's2502', 60, 65); +INSERT INTO `bbb` (`examno`, `stuno`, `writtenexam`, `labexam`) VALUES (3, 's2503', 86, 70); +INSERT INTO `bbb` (`examno`, `stuno`, `writtenexam`, `labexam`) VALUES (4, 's2504', 40, 80); +INSERT INTO `bbb` (`examno`, `stuno`, `writtenexam`, `labexam`) VALUES (5, 's2505', 70, 85); +INSERT INTO `ccc` (`examno`, `stuid`, `score`) VALUES (1, 's2501', 88); +INSERT INTO `ccc` (`examno`, `stuid`, `score`) VALUES (2, 's2501', 92); +INSERT INTO `ccc` (`examno`, `stuid`, `score`) VALUES (3, 's2501', 53); +INSERT INTO `ccc` (`examno`, `stuid`, `score`) VALUES (4, 's2502', 60); +INSERT INTO `ccc` (`examno`, `stuid`, `score`) VALUES (5, 's2502', 55); +INSERT INTO `ccc` (`examno`, `stuid`, `score`) VALUES (6, 's2503', 82); +select *from bbb ; +-- 1.查询出年龄比班上平均年龄大的学生的信息 +SELECT *from aaa where stuage>(select avg(stuage) from aaa); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select stuno,stuname ,stusex ,MAX(score) from aaa INNER join ccc on aaa.stuno =ccc.stuid GROUP BY stuno; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select a.stuno,a.stuname ,a.stusex ,((b.writtenexam + b.labexam)/2) from aaa a left join bbb b on a.stuno = b.stuno; +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from aaa where stuage>20 and stusex='男'; +select * from aaa where (select * from aaa where stuage>20) and stusex='男'; +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from aaa where stuage>(select max(stuage) from aaa where stusex='男') and stusex='女'; +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select * from bbb ,(SELECT ) +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select * from aaa left join ccc on aaa.stuno = ccc.stuid where stuid in (select stuid from ccc where score is not null GROUP BY stuid ) GROUP BY stuno; +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +-- +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +-- +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) + +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select * from aaa,(select stuid from ccc where stuid != '2501' group by stuid having max(score) > (select max(score) from ccc where stuid != '2501')) k where aaa.stuno = k.stuid; +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +-- +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select * from aaa where stuage >(select max(stuage) from aaa where stusex='男' ) and stusex='女' GROUP BY stuno ; +-- 14.查询出只是比某个男生年龄大的女生的信息 +select * from aaa WHERE stuage>(select min(stuage) from aaa WHERE stusex='男') and stusex='女'; \ No newline at end of file -- Gitee