From 4a831afac478201dfb92a93c5965ad9822fb8737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E9=92=8A=E4=BC=9F?= <1319099240@qq.com> Date: Fri, 3 Mar 2023 00:25:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=92=8C=E5=85=B3?= =?UTF-8?q?=E8=81=94=E6=9F=A5=E8=AF=A2=E2=80=9C=20git=20commit=20-m=20?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=92=8C=E5=85=B3=E8=81=94=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\204\344\275\234\344\270\2322023.3.2.md" | 231 ++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 "44\345\217\267--\351\203\235\351\222\212\344\274\237/44\345\217\267\351\203\235\351\222\212\344\274\237\347\232\204\344\275\234\344\270\2322023.3.2.md" diff --git "a/44\345\217\267--\351\203\235\351\222\212\344\274\237/44\345\217\267\351\203\235\351\222\212\344\274\237\347\232\204\344\275\234\344\270\2322023.3.2.md" "b/44\345\217\267--\351\203\235\351\222\212\344\274\237/44\345\217\267\351\203\235\351\222\212\344\274\237\347\232\204\344\275\234\344\270\2322023.3.2.md" new file mode 100644 index 0000000..9c82f2b --- /dev/null +++ "b/44\345\217\267--\351\203\235\351\222\212\344\274\237/44\345\217\267\351\203\235\351\222\212\344\274\237\347\232\204\344\275\234\344\270\2322023.3.2.md" @@ -0,0 +1,231 @@ +# 1.课堂笔记 + +#### 1.函数函数 + +(1)AVG(求平均值) + +(2)count(统计数) + +(3)max(求最大值) + +(4)min(求最小值) + +(5)sum(求和) + +Round(M,N) 保留M的N位小数,小数位超过N会四舍五入 + +curdate() 当前日期 +curtime() 当前时间 +now() 当前日期+时间 +year() 取年 +month() 取月 +day() 取天 + +truncate(M,N) 保留M的N位小数,小数位超过N ,直接截断 不四舍五入 + +例如:求最大的员工过生日 + +``` +select * from 表名称 where birthday=select max(birthday) from 表名称 +``` + +如果不要为空出现: + +``` +WHERE 字段 is not null group by 字段 +``` + +count(x)等价于常量,这两种在统计数量时会把null包含进来。 + +#### 2.关联查询 + +关联查询的结果一共有7种: + +(1)A,只去a的部分,可能包含部分b 左连接 + +``` +SELECT 字段列表 from a表 left b表 on a表.关联字段=b表.关联字段 +``` + + + +(2)A∩B,只取A跟B的共同部分 内连接 + +``` +SELECT 字段列表 from a表 inner join b表 on a表.关联字段=b表.关联字段 +``` + + + +(3)A-A∩B,A的部分—共同部分,只去A独有的 左连接 + +```mysql +SELECT 字段列表 from a表 leftr join b表 on a表.关联字段=b表.关联字段 where a.关联字段 is null +``` + + + +(4)B,只去b的部分,可能包含a的部分 右连接 + +```mysql +SELECT 字段列表 from a表 right join b表 on a表.关联字段=b表.关联字段 +``` + + + +(5)A∪B,就是取所有的 全外连接 + +```mysql +结果A union结果B +``` + + + +(6)B-A∩B,取b部分,只取b部门的 右连接 + +```mysql +SELECT 字段列表 from a表 right join b表 on a表.关联字段=b表.关联字段 where a表.关联字段 is null +``` + + + +(7)A∪B-A∩B,不取共同的,其他的全取 全外连接 + +```mysql +结果A-A∩B union B-A∩B +``` + + + +两个表的记录分为四种: + +(1)左连接 + +(2)右连接 + +(3)全外连接 + +(4)内连接 + +# 2.课后作业 + +```mysql +CREATE DATABASE a; +alter DATABASE a charset utf8; +USE a; +DROP TABLE student; +CREATE TABLE student( +Sno varchar (20) NOT NULL COMMENT'学号' PRIMARY KEY, +Sname varchar (20) NOT NULL COMMENT '学生姓名', +Ssex varchar (20) NOT NULL COMMENT'学生性别', +Sbirthday datetime COMMENT'学生出生年月', +Class varchar (20) COMMENT '学生所在班级' +); +DESC student; +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'); +CREATE TABLE if NOT EXISTS Teacher( +Tno varchar (20) PRIMARY KEY COMMENT'教工编号' , +Tname varchar (20)COMMENT'教工姓名', +Tsex varchar (20)COMMENT'教工性别', +Tbirthday datetime NOT NULL COMMENT'教工出生年月', +Prof varchar (20)NOT NULL COMMENT'职称', +Depart varchar (20)COMMENT'教工所在部门' +); +CREATE TABLE if not EXISTS Course( +Cno varchar (20)NOT NULL PRIMARY KEY COMMENT'课程号', +Cname varchar (20)NOT NULL COMMENT'课程名称', +Tno varchar (20) NOT NULL COMMENT'教工编号', +FOREIGN KEY (Tno) REFERENCES Teacher (Tno) +); +CREATE TABLE Score( +Sno varchar (20)NOT NULL COMMENT'学号', +FOREIGN KEY (Sno) REFERENCES Student (Sno), +Cno varchar (20)NOT NULL COMMENT'课程号', +FOREIGN KEY (Cno) REFERENCES Course (Cno), +Degree Decimal(4,1) COMMENT'成绩' +); + +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'); +#SELECT student.Sno,sname,ssex,sbirthday,class FROM student left JOIN Score ON student.Sno=score.Sno; +#SELECT Score.sno,cno,degree FROM student RIGHT JOIN Score ON student.sno=score.sno; +#SELECT course.tno,cno,cname FROM course left JOIN teacher ON course.tno=teacher.tno; +#SELECT teacher.tno,tname,tsex,tbirthday,prof,depart FROM course RIGHT JOIN teacher ON course.tno=teacher.tno; +#SELECT * FROM student left JOIN Score ON student.Sno=score.Sno UNION SELECT * FROM student RIGHT JOIN Score ON student.sno=score.sno; +SELECT student.*,course.* FROM student INNER JOIN score on student.sno=score.sno LEFT JOIN course ON score.cno=course.cno; +#!!!! +SELECT Teacher.* FROM Teacher LEFT JOIN Course on Teacher.Tno=Course.Tno LEFT JOIN Score on Course.Cno=Score.Cno LEFT JOIN Student on Student.Sno=Score.Sno WHERE Score.Sno is null; +SELECT sno,cno FROM score WHERE degree=(SELECT MAX(degree) FROM score); +SELECT Sname,Cno,Degree FROM student JOIN ; +SELECT student.sno,cno,degree FROM student RIGHT JOIN Score ON student.sno=score.sno; +SELECT score.Sno,Cname,Degree FROM score JOIN course ON course.Cno=score.Cno; +SELECT Cname,Degree,score.Cno FROM course JOIN score ON course.Cno=score.Cno; +SELECT sname ,Degree,student.Sno FROM student JOIN score ON student.Sno=score.Sno; +SELECT Cname,Degree,sname FROM student JOIN Score ON student.Sno=score.Sno JOIN course ON course.Cno=score.Cno; +#查询“95033”班学生的平均分。 +SELECT ROUND(avg(degree),2) FROM score WHERE sno IN (SELECT sno FROM student WHERE class='95033'); +#查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录 +SELECT * FROM student,score WHERE score.Cno='3-105' AND student.Sno=score.Sno AND score.Degree>(SELECT degree FROM score WHERE score.Cno='3-105' AND score.Sno='109'); +# 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +SELECT * FROM score WHERE degree<(SELECT MAX(degree) FROM score) +#⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +SELECT * FROM student,score WHERE student.Sno=score.Sno AND student.Sno=score.Sno AND score.Degree>(SELECT degree FROM score WHERE score.Sno='109' AND score.Cno='3-105'); +#⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +SELECT Sno,Sname,Sbirthday FROM student WHERE YEAR(student.sbirthday)=(SELECT YEAR(sbirthday) FROM student WHERE sno='108'); +#⑩ 查询“张旭“教师任课的学生成绩。 +SELECT degree FROM teacher,student,score,course WHERE teacher.Tname='张旭' AND teacher.Tno=course.Tno AND score.Cno=course.Cno; +#⑪ 查询选修某课程的同学人数多于5人的教师姓名。 +SELECT Tname from Teacher where Tno in (SELECT Tno FROM Course where Cno in(SELECT Cno FROM Score GROUP BY Cno HAVING COUNT(*)>5)); +#⑫ 查询出“计算机系“教师所教课程的成绩表。!! +SELECT * FROM score WHERE cno IN(SELECT cno FROM course WHERE tno IN(SELECT tno FROM teacher WHERE depart='计算机系')); +#⑬ 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +SELECT tname,prof FROM teacher where prof not in (SELECT prof FROM teacher WHERE teacher.Depart!=teacher.Depart); +#⑭ 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +SELECT sno,cno,degree FROM score a WHERE (SELECT degree FROM score b WHERE cno='3-105' AND a.Sno=b.Sno)>=(SELECT degree from score c WHERE cno='3-245' and a.Sno=c.Sno) order by degree desc; +#⑮ 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.!!!! +SELECT sno,cno,degree FROM score where degree>(SELECT MAX(degree) from score where cno='3-245')and cno='3-105'; +#⑯ 查询成绩比该课程平均成绩低的同学的成绩表。 +SELECT sno,cno,degree FROM score WHERE score.Degree<(SELECT AVG(degree)FROM score WHERE score.Cno=score.Cno); +#⑰ 查询所有任课教师的Tname和Depart.!! +SELECT tname,deprat FROM teacher WHERE Tno IN (SELECT tno FROM course WHERE cno IN(SELECT DISTINCT cno FROM score)); +#⑱ 查询所有未讲课的教师的Tname和Depart. +SELECT Tname,Depart FROM teacher WHERE Tname not in (SELECT distinct Tname from Teacher,Course,Score where Teacher.Tno=Course.Tno and Course.Cno=Score.Cno); +#⑲ 查询“男”教师及其所上的课程。!!! +SELECT tname,cname FROM teacher,course WHERE tsex='男' AND teacher.Tno=course.Tno; +#⑳ 查询最高分同学的Sno、Cno和Degree列。 +SELECT Sno,Cno,Degree FROM score WHERE degree=(SELECT MAX(degree) FROM score); +#查询和“李军”同性别的所有同学的Sname +SELECT * FROM student WHERE ssex=(SELECT ssex FROM student WHERE sname='李军') AND sname not in('李军'); +#22 查询和“李军”同性别并同班的同学Sname. +SELECT * FROM student WHERE ssex=(SELECT ssex from student WHERE sname='李军') AND sname not in('李军') AND class=(SELECT class FROM student WHERE sname='李军'); +#23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 +SELECT * FROM score WHERE sno IN(SELECT sno FROM student WHERE ssex='男') AND cno in(SELECT cno from course WHERE cname='计算机导论'); +``` + -- Gitee From 0dbfffdb30b9f6e6a2b2d6ae7bdce37e2c38044a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E9=92=8A=E4=BC=9F?= <1319099240@qq.com> Date: Fri, 10 Mar 2023 00:00:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?44=E5=8F=B7=E9=83=9D=E9=92=8A=E4=BC=9F?= =?UTF-8?q?=E7=9A=84=E4=BD=9C=E4=B8=9A20230306?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\204\344\275\234\344\270\2322023.3.6.sql" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 "44\345\217\267--\351\203\235\351\222\212\344\274\237/44\345\217\267\351\203\235\351\222\212\344\274\237\347\232\204\344\275\234\344\270\2322023.3.6.sql" diff --git "a/44\345\217\267--\351\203\235\351\222\212\344\274\237/44\345\217\267\351\203\235\351\222\212\344\274\237\347\232\204\344\275\234\344\270\2322023.3.6.sql" "b/44\345\217\267--\351\203\235\351\222\212\344\274\237/44\345\217\267\351\203\235\351\222\212\344\274\237\347\232\204\344\275\234\344\270\2322023.3.6.sql" new file mode 100644 index 0000000..ec64550 --- /dev/null +++ "b/44\345\217\267--\351\203\235\351\222\212\344\274\237/44\345\217\267\351\203\235\351\222\212\344\274\237\347\232\204\344\275\234\344\270\2322023.3.6.sql" @@ -0,0 +1,86 @@ +```mysql +drop database if EXISTS zuoyelianxi; +create database zuoyelianxi CHARSET utf8; +use zuoyelianxi; +drop table if exists stuinfo; +create table stuinfo( +stuNO VARCHAR(20), +stuName VARCHAR(20), +stuSex enum('男','女'), +stuAge int(2), +stuSddress VARCHAR(20), +stuSeat int(1) +); +CREATE table stuExam( +examNO int(1), +stuNO varchar(20), +writtenExam INT(20), +labExam int(2) +); +create table stuMarks( +examNo int(1), +stuID VARCHAR(20), +scone int(2) +); +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','梅超风','男',16,'湖北武汉',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(2,'s2501',92); +insert into stuMarks VALUES(3,'s2501',53); +insert into stuMarks VALUES(4,'s2502',60); +insert into stuMarks VALUES(5,'s2502',99); +insert into stuMarks VALUES(6,'s2503',82); +select * from stuinfo; +select * from stuExam; +select * from stuMarks; +-- 1.查询出年龄比班上平均年龄大的学生的信息 +select avg(stuAge) from stuinfo; +select * from stuinfo where stuAge>(select avg(stuAge) from stuinfo); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select stuID,max(scone) c from stuMarks GROUP BY stuID; +select stuNO,stuName,stuSex,ifnull(c,0) from stuinfo a left join (select stuID,max(scone) c from stuMarks GROUP BY stuID) d on a.stuNO=d.stuID; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select stuNO,(writtenExam+labExam)/2 e from stuexam GROUP BY stuNo; + +select a.stuNO,stuName,stuSex,e from stuinfo a left join (select stuNO,(writtenExam+labExam)/2 e from stuexam GROUP BY stuNo) f on a.stuNO=f.stuNO; +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuSex='男' and stuAge>=20; +select stuNO,stuName,stuSex from stuinfo where stuSex='男'; +select * from stuinfo where stuAge>=20 and stuSex=any(select stuSex from stuinfo where stuSex='男'); +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select stuNO,max(stuAge) from stuinfo where stuSex='男'; +select * from stuinfo where stuAge>(select max(stuAge) from stuinfo where stuSex='男'); +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select a.* from stuinfo a join stumarks b on a.stuNO=b.stuID where stuNO not in(select stuNO FROM stuinfo JOIN stumarks on stuinfo.stuNO=stumarks.stuID WHERE scone<60) GROUP BY stuNO; +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.*,b.scone from stuinfo a left join stumarks b on a.stuNO=b.stuID where scone is not null GROUP BY stuNO; +select * from stuinfo where stuNo in (select stuNO from stuinfo a left join stumarks b on a.stuNO=b.stuID where scone is not null GROUP BY stuNO); +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.*,b.scone from stuinfo a left join stumarks b on a.stuNO=b.stuID where scone is null GROUP BY stuNO; +select * from stuinfo where stuNo in (select stuNO from stuinfo a left join stumarks b on a.stuNO=b.stuID where scone is null GROUP BY stuNO); +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select a.*,b.scone from stuinfo a left join stumarks b on a.stuNO=b.stuID where scone>90 GROUP BY stuNO; +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuexam) +select stuNO,(writtenExam+labExam)/2 e from stuexam GROUP BY stuNo; +select a.*,h.e from stuinfo a left join (select stuNO,(writtenExam+labExam)/2 e from stuexam GROUP BY stuNo) h on a.stuNO=h.stuNO where e>80; +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select * FROM stuinfo LEFT JOIN stumarks on stuinfo.stuNO=stumarks.stuID WHERE scone >all(SELECT scone FROM stuinfo LEFT JOIN stumarks on stuinfo.stuNO=stumarks.stuID WHERE stuName='张秋利'); +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select a.* from stuinfo a left join stumarks b on a.stuNO=b.stuID where scone>any(select scone from stuinfo left join stumarks on stuinfo.stuNO=stumarks.stuID where stuName='张秋利') GROUP BY stuNO; +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select stuNO,max(stuAge) from stuinfo where stuSex='男'; +select * from stuinfo where stuAge>(select max(stuAge) from stuinfo where stuSex='男') and stuSex='女'; +-- 14.查询出只要比某个男生年龄大的女生的信息 +select * from stuinfo where stuAge>any(select stuAge from stuinfo where stuSex='男') and stuSex='女'; +``` + -- Gitee