diff --git "a/26 \351\231\210\346\231\223\347\201\277/20230228 \351\231\210\346\231\223\347\201\277\345\205\263\350\201\224\346\237\245\350\257\242.md" "b/26 \351\231\210\346\231\223\347\201\277/20230228 \351\231\210\346\231\223\347\201\277\345\205\263\350\201\224\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..f333508702115ff94d70b04e09cf5ec773b5b239 --- /dev/null +++ "b/26 \351\231\210\346\231\223\347\201\277/20230228 \351\231\210\346\231\223\347\201\277\345\205\263\350\201\224\346\237\245\350\257\242.md" @@ -0,0 +1,225 @@ +# 笔记 + +联查询:两个或更多个表一起查询。 +又称为联合查询,多表查询。 + +### 1、关联查询的结果一共有7种: + +**两个表的记录分为四种: +①A表中的记录能在B表中找到对应的记录 +②A表中的记录在B表中找不到对应的记录 +③B表中的记录可以在A表找到对应的记录 +④B表中的记录在A表中找不到对应的记录 + +(1)A∩B ①③ +(2)A ①② +(3)A-A∩B ② +(4)B ③④ +(5)B-A∩B ④ +(6)A∪B ①②③④ +(7)A∪B-A∩B ②④** + +### 2、查询 + +两个表要一起查询,要有前提条件:有关联 +就是有相同逻辑意义和数据类型的字段。 + +### 3、如何实现7种查询结果 + +```sql +(1)内连接 inner join +(2)外连接 outer join +左外连接 left outer join 或 left join +右外连接 right outer join 或 right join +全外连接 full outer join 或 full join -- union 代替 +``` + +```sql +但是,mysql不支持全外连接,没有full join。 +mysql使用union关键字合并其他的查询结果实现全外连接的效果。 +``` + +```sql +内连接 ==> A∩B +左连接 ==> A 或 A-A∩B +右连接 ==> B 或 B-A∩B +全外连接 ==> A∪B 或 A∪B - A∩B + 左连接的A union 右连接的B 得到 A∪B + 左连接的A-A∩B union 右连接B-A∩B 得到 A∪B - A∩B +``` + + + +### 4、内连接 inner join + +```sql +A表 inner join B表 on 关联条件 +``` + + + +### 5、左连接 left join + +```sql +(1)A表:A表 left join B表 on A表.关联字段 = B表.关联字段 +(2)A-A∩B: + A表 left join B表 on A表.关联字段 = B表.关联字段 + where 从表.关联字段 is null + 从表是A和B 看占从表位置的表。 +``` + + + +# 作业 + +```sql +create database zy1 charset utf8; +use zy1; + +create table student( + sno varchar(20)primary key, + sname varchar(20)not null, + ssex varchar(20)not null, + sbirthday datetime, + class varchar(20) +); + +create table course( + cno varchar(20)primary key, + cname varchar(20) not null, + tno varchar(20)not null, + foreign key (tno) references teacher(tno) +); + +create table score( + sno varchar(20)not null, + cno varchar(20)not null, + degree decimal(4,1), + foreign key (sno) references student(sno), + foreign key (cno) references course(cno) +); + +create table teacher( + tno varchar(20)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'); +insert into student values(105,'匡明','男','1975-10-2','95031'); +insert into student values(107,'王丽','女','1976-1-23','95033'); +insert into student values(101,'李军','男','1976-2-20','95033'); +insert into student values(109,'王芳','女','1975-2-10','95031'); +insert into student values(103,'陆君','男','1974-6-3','95031'); + +insert into course values('3-105','计算机导论','825'); +insert into course values('3-245','操作系统','804'); +insert into course values('6-166','数字电路','856'); +insert into course values('9-888','高等数学','831'); + +insert into score values('103','3-245','86'); +insert into score values('105','3-245','75'); +insert into score values('109','3-245','68'); +insert into score values('103','3-105','92'); +insert into score values('105','3-105','88'); +insert into score values('109','3-105','76'); +insert into score values('101','3-105','64'); +insert into score values('107','3-105','91'); +insert into score values('108','3-105','78'); +insert into score values('101','6-166','85'); +insert into score values('107','6-166','79'); +insert into score values('108','6-166','81'); + +insert into teacher values('804','李诚','男','1958-12-2','副教授','计算机系'); +insert into teacher values('856','张旭','男','1969-3-12','讲师','电子工程系'); +insert into teacher values('825','王萍','女','1972-5-5','助教','计算机系'); +insert into teacher values('831','刘冰','女','1977-8-14','助教','电子工程系'); + +-- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ +select student.sno,sname,ssex,sbirthday,class,cname +from student left join score on student.sno=score.sno left join course on score.cno=course.cno; +-- 2,查询没有学生的教师的所有信息\ +SELECT * from course,score,teacher +where (teacher.tno=course.tno AND course.cno=score.cno); + +-- 4.查询 +-- ① 查询Score表中的最高分的学生学号和课程号。 +select sno,cno from score where degree=(select max(degree) from score); +-- ② 查询所有学生的Sname、Cno和Degree列。 +select sname,score.cno,degree from student inner join score on student.sno=score.sno; +-- ③ 查询所有学生的Sno、Cname和Degree列。 +select score.sno,cname,degree from score inner join course on score.cno=course.cno; +-- ④ 查询所有学生的Sname、Cname和Degree列。 +select sname,cname,degree from student inner join score on student.sno=score.sno inner join course on score.cno=course.cno; +-- ⑤ 查询“95033”班学生的平均分。 +select avg(score.Degree) from score inner join student on student.Sno = score.Sno where student.Class = '95033'; +-- ⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select * from student inner join score on student.sno=score.sno where score.cno='3-105' and degree > (select degree from score where score.sno=109 and score.cno='3-105' ); +-- ⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select max(score.degree) from score where 1< (select count(sno) from score) +select count(sno) from score group by sno +-- ⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from score +where 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 sbirthday=(select sbirthday from student where sno=108); +-- 这个只有他自己生日一样下面是不输出他自己生日的代码 +select sno,sname,sbirthday from student +where sbirthday=(select sbirthday from student where sno=108)and sno!=108; +-- ⑩ 查询“张旭“教师任课的学生成绩。 +select degree from score left join course +on score.cno=course.cnoleft join teacher on course.tno=teacher.tno where score.cno=(select course.cno from course left join teacher on course.tno=teacher.tno where tname='张旭') +-- ⑪ 查询选修某课程的同学人数多于5人的教师姓名。 +select tname from teacher left join course +on teacher.tno=course.tno +left join score +on course.cno=score.cno +where (select count(score.cno) from score group by cno ) +-- ⑫ 查询出“计算机系“教师所教课程的成绩表。 +select score.sno,score.cno,degree from score left join course on score.cno=course.cno left join teacher on course.tno=teacher.tno where depart='计算机系'; + +-- ⑬ 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select tname,prof from teacher where prof in (select distinct prof from teacher); +-- ⑭ 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 + +select score.cno,score.sno,score.degree from +score inner join course on score.cno = course.cno and score.cno = '3-105' and score.degree > (select max(degree) from score where score.cno='3-245') order by score.degree desc; +-- ⑮ 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select score.cno,score.sno,score.degree from +score inner join course on score.cno = course.cno and score.cno = '3-105' and score.degree > (select max(degree) from score where score.cno='3-245'); +-- ⑯ 查询成绩比该课程平均成绩低的同学的成绩表。 +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); +-- ⑰ 查询所有任课教师的Tname和Depart. +select tname,depart from teacher where Tname in (select distinct Tname from teacher,course,score where Teacher.Tno=course.tno and course.cno=score.cno); +-- ⑱ 查询所有未讲课的教师的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 left join course +on teacher.tno=course.tno +where tsex='男'; +-- ⑳ 查询最高分同学的Sno、Cno和Degree列。 +select sno,cno,degree from score where degree=( +select max(degree) 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 score.* from score left join course +on score.cno=course.cno +left join student +on score.sno=student.sno +where score.cno=(select course.cno from course where cname='计算机导论') + +and ssex='男'; +-- +``` +