diff --git "a/04 \345\217\266\346\246\225\351\224\213/2023.3.5\346\237\245\350\257\242\350\277\236\346\216\245.md" "b/04 \345\217\266\346\246\225\351\224\213/2023.3.5\346\237\245\350\257\242\350\277\236\346\216\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..0b0270bb6f2224a1d90f3cbc4858e4cfd77d6bc0 --- /dev/null +++ "b/04 \345\217\266\346\246\225\351\224\213/2023.3.5\346\237\245\350\257\242\350\277\236\346\216\245.md" @@ -0,0 +1,269 @@ +# 笔记: + +#### 一、关联查询: + +关联查询: +当select语句基于两个或更多个表一起查询时,称为关联查询,或联合查询。 + +1、联合查询的结果有7种情况: +(1)A∩B +(2)A +(3)A - A∩B +(4)B +(5)B - A∩B +(6)A∪B +(7)A∪B - A∩B + +2、关联查询有几种情况,分别实现什么结果 +(1)内连接:INNER JOIN +实现结果:A∩B + +(2)左外连接,简称左连接:A LEFT JOIN B +实现结果:A 或 A - A∩B + +(3)右外连接,简称右连接:A RIGHT JOIN B +实现结果:B 或 B - A∩B + +(4)全外连接,简称全连接: A FULL JOIN B(但是mysql不支持这种写法) +Mysql通过合并左连接和右连接的结果实现全连接的效果。 + + +实现A结果的左连接 union 实现B结果的右连接 = A∪B + +实现A - A∩B结果的左连接 union 实现B - A∩B结果的右连接 = A∪B - A∩B + +#### 1.内连接: + +观察数据: +t_employee 看成A表 +t_department 看成B表 +此时t_employee (A表)中有 李红和周洲的did是NULL,没有对应部门, + t_department(B表)中有 测试部,在员工表中找不到对应记录的。 + +#查询所有员工的姓名,部门编号,部门名称 +#如果员工没有部门的,不要 +#如果部门没有员工的,不要 + +员工的姓名在t_employee (A表)中 +部门的编号,在t_employee (A表)和t_department(B表)都有 +部门名称在t_department(B表)中 +所以需要联合两个表一起查询。 + +(1)凡是联合查询的两个表,必须有“关联字段”, +关联字段是逻辑意义一样,数据类型一样,名字可以一样也可以不一样的两个字段。 +比如:t_employee (A表)中did和t_department(B表)中的did。 + +发现关联字段其实就是可以建外键的字段。当然联合查询不要求一定建外键。 + +(2)联合查询必须写关联条件,关联条件的个数 = n - 1. +n是联合查询的表的数量。 +如果2个表一起联合查询,关联条件数量是1, +如果3个表一起联合查询,关联条件数量是2, +如果4个表一起联合查询,关联条件数量是3, +。。。。 +否则就会出现笛卡尔积现象,这是应该避免的。 + +(3)关联条件可以用on子句编写,也可以写到where中。 +但是建议用on单独编写,这样呢,可读性更好。 + +每一个join后面都要加on子句 +A inner|left|right join B on 条件 +A inner|left|right join B on 条件 inner|left|right jon C on 条件 + +#### 2.左连接: + +t_employee 看成A表 +t_department 看成B表 +此时t_employee (A表)中有 李红和周洲的did是NULL,没有对应部门, + t_department(B表)中有 测试部,在员工表中找不到对应记录的。 + +#### 3.有链接 + +t_employee 看成A表 +t_department 看成B表 +此时t_employee (A表)中有 李红和周洲的did是NULL,没有对应部门, + t_department(B表)中有 测试部,在员工表中找不到对应记录的。 + +查询的结果是B - A∩B A right join B +此时的where条件,建议写子表的关联字段is null,这样更准确一点。 +如果要建外键,它们肯定有子表和父表的角色,写子表的关联字段is null +因为父表中这个字段一般是主键,不会为null。 + +#### 4.自连接: + +演示特殊的联合查询/关联查询/多表查询:自连接 +物理上,是一张表 +逻辑上,是两张表 +分析表结构:t_employee表 +mid:是表示存储员工的领导编号。即该员工归谁管。 + 领导编号其中就是“领导”作为员工的员工编号 + 例如:eid为3的员工邓超远,他的mid是7,表示他的领导是员工编号为7的员工。 + eid为7的员工是贾宝玉,他的eid是7,贾宝玉作为员工来说,他的编号是7, + 作为领导来说,他的编号也是7。 + +mid的取值范围受到eid字段的限制。mid的值选择必须是eid有的值范围。 + +可以理解为mid和eid是关联字段,如果要建外键,可以在mid字段上建外键。 +foreign key(mid) references t_employee(eid) + +此时t_employee既是子表也是父表。 +员工表t_employee建立了外键: +CONSTRAINT `t_employee_ibfk_3` FOREIGN KEY (`mid`) REFERENCES `t_employee` (`eid`) ON DELETE SET NULL ON UPDATE CASCADE + +#### 二、分组函数: + +1.统计 count + +2.最大值 max + +3.最小值 min + +4.求和 sum + +5.平均值 avg + +## 作业 + +```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='男'; +``` \ No newline at end of file diff --git "a/04 \345\217\266\346\246\225\351\224\213/20230306 \347\273\203\344\271\240\344\275\234\344\270\232.md" "b/04 \345\217\266\346\246\225\351\224\213/20230306 \347\273\203\344\271\240\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..c80aa3b7a019a47277268c78fb3eecf4413a59fd --- /dev/null +++ "b/04 \345\217\266\346\246\225\351\224\213/20230306 \347\273\203\344\271\240\344\275\234\344\270\232.md" @@ -0,0 +1,111 @@ +```mysql +CREATE DATABASE lianxi charset utf8; +use lianxi; +CREATE table stuinfo( +stuNO VARCHAR(10) primary key, +stuName VARCHAR(10), +stuSex VARCHAR(10), +stuAge VARCHAR(10), +stuAddress VARCHAR(20), +stuSeat INT +); +CREATE table stuExam( +examNo int primary key auto_increment, +stuNO VARCHAR(10), +writtenExam int, +labExam INT +); +CREATE table stuMarks( +examNo int primary key auto_increment, +stuID VARCHAR(10), +score INT +); +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','马文才','男',18,'湖南长沙',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), +(5,'s2505',70,85), +(6,'s2506',85,90); + +INSERT into stuMarks VALUES +(1,'s2501',88), +(2,'s2501',92), +(3,'s2501',53), +(4,'s2502',60), +(5,'s2502',99), +(6,'s2503',82); + + +# 1.查询出年龄比班上平均年龄大的学生的信息 +select * from stuinfo where stuAge>(select avg(stuAge) from stuinfo); + +# 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select a.stuNO,a.stuName,a.stuSex,max(score) from stuinfo a left join stumarks b on a.stuNO=b.stuID group by stuNO; + +# 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select a.stuNO,a.stuName,a.stuSex,(b.writtenExam+b.labExam)/2 平均分 from stuinfo a left join stuexam b on a.stuNO=b.stuNO; + +# 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuSex='男' and stuAge>=20; + +# 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stuSex='女' and stuAge>(select max(stuAge) from stuinfo where stuSex='男'); + +# 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select a.* from stuinfo a, (select stuID from stumarks group by stuID having min(score)>=60) b where a.stuNO=b.stuID; + +# 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select *from stuinfo st ,(select stuID from stumarks group by stuID ) sk +where +st.stuNO =sk.stuID; + +# 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select *from stuinfo where stuNO not in(select stuID from stumarks group by stuID) + + +# 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select st.* from stuinfo st,(select stuID from stumarks group by stuID having max(score)>90 ) sk +where +st.stuNO=sk.stuID; + +# 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select *from stuinfo st, (select stuID from stumarks group by stuID having avg(score)>80) +sk WHERE +st.stuNO=sk.stuID; +# 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select st.*from stuinfo st, +(select stuID from stumarks where stuID !='s2501' group by stuID having max(score)> +(select max(score) form stumarks where stuID='s2501')) sk where +st.stuNO=sk.stuID; + +# 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select st.*from stuinfo st, +(select stuID from stumarks WHERE stuID !='s2501'group by stuID having max(score)> +(select min(score) from stumarks WHERE stuID='s2501' )) sk +where +st.stuNO=sk.stuID; + +# 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