From 3d80e3133c561c6b6851571471d5c426aefcb7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E9=9B=AA=E7=90=BC?= <3403205493@qq.com> Date: Tue, 28 Feb 2023 23:30:12 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E2=80=9C=E7=94=B0=E9=9B=AA=E7=90=BC?= =?UTF-8?q?=E7=9A=84=E4=BD=9C=E4=B8=9A=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...63\350\201\224\346\237\245\350\257\242.md" | 214 ++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 "35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" diff --git "a/35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" "b/35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" new file mode 100644 index 0000000..5893b84 --- /dev/null +++ "b/35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" @@ -0,0 +1,214 @@ +# 笔记 + +```mysql + + +1、内连接:inner join ... on + +结果:A表 ∩ B表 + +2、左连接:A left join B on + +(2)A表全部 + +(3)A表- A∩B + +3、右连接:A right join B on + +(4)B表全部 + +(5)B表-A∩B + +4、全外连接:full outer join ... on,但是mysql不支持这个关键字,mysql使用union(合并)结果的方式代替 + +(6)A表∪B表: (2) A表结果 union (4)B表的结果 + +(7)A∪B - A∩B (3)A表- A∩B结果 union (5)B表-A∩B结果 +``` + + + +# 作业 + +```mysql +create database zuoye2 charset utf8; +use zuoye2; + +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 tname from +teacher left join course on teacher.tno=course.tno +left join score on course.cno=score.cno +left join student on score.sno=student.sno +where min(lishi( +select count(score.cno),cname as rshu from +score right join course on score.cno=course.cno group by course.cno)); +-- 不会做别抄 + +select * from teacher; +select * from score; + +select * from course; +-- + +-- 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中选学多门课程的同学中分数为非最高分成绩的记录。 + +-- ⑧ 查询成绩高于学号为“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.cno +left 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 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'); +-- ⑯ 查询成绩比该课程平均成绩低的同学的成绩表。 + +-- ⑰ 查询所有任课教师的Tname和Depart. + +-- ⑱ 查询所有未讲课的教师的Tname和Depart. + +-- ⑲ 查询“男”教师及其所上的课程。 +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='男'; +-- +``` + -- Gitee From 8cc22aad9b684d08baf7cfb5ac1987006a56bf96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E9=9B=AA=E7=90=BC?= <11785247+tian-xueqiong@user.noreply.gitee.com> Date: Wed, 1 Mar 2023 08:20:52 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2035?= =?UTF-8?q?=20=E7=94=B0=E9=9B=AA=E7=90=BC/2023228=20=E5=85=B3=E8=81=94?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...63\350\201\224\346\237\245\350\257\242.md" | 214 ------------------ 1 file changed, 214 deletions(-) delete mode 100644 "35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" diff --git "a/35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" "b/35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" deleted file mode 100644 index 5893b84..0000000 --- "a/35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" +++ /dev/null @@ -1,214 +0,0 @@ -# 笔记 - -```mysql - - -1、内连接:inner join ... on - -结果:A表 ∩ B表 - -2、左连接:A left join B on - -(2)A表全部 - -(3)A表- A∩B - -3、右连接:A right join B on - -(4)B表全部 - -(5)B表-A∩B - -4、全外连接:full outer join ... on,但是mysql不支持这个关键字,mysql使用union(合并)结果的方式代替 - -(6)A表∪B表: (2) A表结果 union (4)B表的结果 - -(7)A∪B - A∩B (3)A表- A∩B结果 union (5)B表-A∩B结果 -``` - - - -# 作业 - -```mysql -create database zuoye2 charset utf8; -use zuoye2; - -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 tname from -teacher left join course on teacher.tno=course.tno -left join score on course.cno=score.cno -left join student on score.sno=student.sno -where min(lishi( -select count(score.cno),cname as rshu from -score right join course on score.cno=course.cno group by course.cno)); --- 不会做别抄 - -select * from teacher; -select * from score; - -select * from course; --- - --- 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中选学多门课程的同学中分数为非最高分成绩的记录。 - --- ⑧ 查询成绩高于学号为“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.cno -left 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 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'); --- ⑯ 查询成绩比该课程平均成绩低的同学的成绩表。 - --- ⑰ 查询所有任课教师的Tname和Depart. - --- ⑱ 查询所有未讲课的教师的Tname和Depart. - --- ⑲ 查询“男”教师及其所上的课程。 -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='男'; --- -``` - -- Gitee From 2db10c692e5e83b9feec7adfc06233011ee50f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E9=9B=AA=E7=90=BC?= <3403205493@qq.com> Date: Wed, 8 Mar 2023 11:49:35 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E7=94=B0=E9=9B=AA=E7=90=BC=E7=9A=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\345\222\214\347\254\224\350\256\260.md" | 128 +++++++++++ ...63\350\201\224\346\237\245\350\257\242.md" | 213 ++++++++++++++++++ 2 files changed, 341 insertions(+) create mode 100644 "35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" create mode 100644 "35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" diff --git "a/35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" "b/35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" new file mode 100644 index 0000000..e0c8135 --- /dev/null +++ "b/35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" @@ -0,0 +1,128 @@ +# 笔记 + +什么子查询 + +-- 嵌套在其它SQL语句中的select语句 +-- 子查询通常在外层语句之前优先执行,所以要用()包括起来 +-- 子查询的结果,一般作为外层语句的条件,数据源等 + +select ① from ③ where ②; + +### 形式一,select后面嵌套子查询 + +where 后面不支持聚合函数和别名,having支持聚合也支持别名 + +### 形式二,在where/having 后面做条件 + +当子查询放在where 、 having 后面当条件用是,有几种情况 + ① 单列单个值,可以直接用>,<,<=...!=,这些比较运算符 + ② 单列多个值,要用in,not in 这种集合的比较 + ③ 单列多个值,可以用>,<,<=...!=,比较运算符,搭配any,all 这些关键字,一起使用 + +### 形式三,在from后面,形成一个临时表,必须加一个别名 + +子查询的结果: +单列单个值:可以放在select 后,也可以放where/having之后 +单列多个值:where后,或having后 ,不用直接用<,=,>= 这种单纯的比较运算符。但可以用搭配any(任意一个),all(所有的)等关键字一起使用。还可以用in,not in这种表达式 + +多列时,只能当临时表来表用,放在from后面,而且必须,给他取个别名 + +# 作业 + +```mysql + + +create database xue charset utf8; +use xue; +drop table stuinfo; +create table stuinfo( +stuno varchar(10), +stuname varchar(10), +stusex varchar(10), +stuage varchar(10), +stuaddress varchar(10), +stuseat varchar(10) +); +desc stuinfo; +insert into stuinfo values +('s2501','张秋利','男','20','美国硅谷','1'), +('s2502','李斯文','女','18','湖北武汉','2'), +('s2503','马文才','男','18','湖南长沙','3'), +('s2504','欧阳俊雄','女','21','湖北武汉','4'), +('s2505','梅超风','男','20','湖北武汉','5'), +('s2506','陈旋风','男','19','美国硅谷','6'); +select * from stuinfo; +create table stuexam( +examno int, +stuno varchar(10), +writtenexam varchar(10), +labexam varchar(10) + +); +desc stuinfo; +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'); +select * from stuinfo; +create table stumarks( +examno int, +stuld varchar(10), +score varchar(10) +); +desc stuinfo; +insert into stumarks values +(1,'s2501','88'), +(2,'s2501','92'), +(3,'s2501','53'), +(4,'s2502','60'), +(5,'s2502','99'), +(6,'s2503','82'); +select * from stuinfo; + +-- 在如图的数据表上完成以下题目 +-- + +-- 1.查询出年龄比班上平均年龄大的学生的信息 +select * from stuinfo where stuage>(select avg(stuage)from stuinfo); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select stuno,stuinfo.stuname,stusex,max(score) from stuinfo INNER JOIN stumarks on stuinfo.stuno=stumarks.stuld GROUP BY stuld; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select * from stuinfo LEFT JOIN stuexam on stuinfo.stuno=stuexam.stuno GROUP BY stuname; +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stusex='男' and stuage>=20; +select * from +(select * from stuinfo where stusex='男') +where stuage>=20; + +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stusex='女' AND stuage>ALL(select stuAge from stuinfo where stusex='男'); +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select * from stuinfo a INNER JOIN stumarks b on a.stuNo = b.stuID where a.stuNo != (select stuid from stumarks where score<60); +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +SELECT * FROM stuinfo a inner join stumarks b on a.stuNo = b.stuID; +select * from stuinfo a inner join (select DISTINCT stuid from stumarks) b on a.stuNo = b.stuid; +select * FROM stuinfo where stuno in (select DISTINCT stuid from stumarks); +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +SELECT * from stuinfo a left join stumarks b on a.stuNo = b.stuID where score is null; +select * from stuinfo where stuno not in (SELECT DISTINCT stuid FROM stuMarks); +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select * from stuinfo where stuno in (select stuid from stumarks where score>90); +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select * from stuinfo where stuno = (select stuno from stuexam where (writtenExam+labExam)/2>80); +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select * from stuinfo a left join stuExam b on a.stuNo = b.stuNo where writtenExam>(select writtenExam from stuExam where stuNo=(select stuNo from stuinfo where stuname='张秋利')) AND labExam>(select labExam from stuExam where stuNo=(select stuNo from stuinfo where stuname='张秋利')); +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select * from stuinfo a left join stuExam b on a.stuNo = b.stuNo where writtenExam>(select writtenExam from stuExam where stuNo=(select stuNo from stuinfo where stuname='张秋利')) OR labExam>(select labExam from stuExam where stuNo=(select stuNo from stuinfo where stuname='张秋利')); +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo where stuSex='女' and stuAge >ALL(select stuAge from stuinfo where stusex='男'); +-- 14.查询出只是比某个男生年龄大的女生的信息 +select * from stuinfo where stuSex='女' AND stuAge >any(select stuage from stuinfo where stusex='男'); +select * from stuinfo where stuSex='女' AND stuAge >(select MIN(stuAge) from stuinfo where stusex='男'); + + +``` + diff --git "a/35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" "b/35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" new file mode 100644 index 0000000..88373e1 --- /dev/null +++ "b/35 \347\224\260\351\233\252\347\220\274/2023228 \345\205\263\350\201\224\346\237\245\350\257\242.md" @@ -0,0 +1,213 @@ +# 笔记 + +```mysql + + +1、内连接:inner join ... on + +结果:A表 ∩ B表 + +2、左连接:A left join B on + +(2)A表全部 + +(3)A表- A∩B + +3、右连接:A right join B on + +(4)B表全部 + +(5)B表-A∩B + +4、全外连接:full outer join ... on,但是mysql不支持这个关键字,mysql使用union(合并)结果的方式代替 + +(6)A表∪B表: (2) A表结果 union (4)B表的结果 + +(7)A∪B - A∩B (3)A表- A∩B结果 union (5)B表-A∩B结果 +``` + + + +# 作业 + +```mysql +create database zuoye2 charset utf8; +use zuoye2; + +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 tname from +teacher left join course on teacher.tno=course.tno +left join score on course.cno=score.cno +left join student on score.sno=student.sno +where min(lishi( +select count(score.cno),cname as rshu from +score right join course on score.cno=course.cno group by course.cno)); + +select * from teacher; +select * from score; + +select * from course; +-- + +-- 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中选学多门课程的同学中分数为非最高分成绩的记录。 + +-- ⑧ 查询成绩高于学号为“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.cno +left 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 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'); +-- ⑯ 查询成绩比该课程平均成绩低的同学的成绩表。 + +-- ⑰ 查询所有任课教师的Tname和Depart. + +-- ⑱ 查询所有未讲课的教师的Tname和Depart. + +-- ⑲ 查询“男”教师及其所上的课程。 +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='男'; +-- +``` + -- Gitee From ca0d92960d29c46bfb4cf20acd3267e6cf001e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E9=9B=AA=E7=90=BC?= <3403255493@qq.com> Date: Wed, 8 Mar 2023 12:12:35 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=94=B0=E9=9B=AA=E7=90=BC=E7=9A=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230306 \345\255\220\346\237\245\350\257\242.md" | 1 - 1 file changed, 1 deletion(-) rename "35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" => "35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242.md" (99%) diff --git "a/35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" "b/35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242.md" similarity index 99% rename from "35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" rename to "35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242.md" index e0c8135..912a1b7 100644 --- "a/35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" +++ "b/35 \347\224\260\351\233\252\347\220\274/20230306 \345\255\220\346\237\245\350\257\242.md" @@ -31,7 +31,6 @@ where 后面不支持聚合函数和别名,having支持聚合也支持别名 ```mysql - create database xue charset utf8; use xue; drop table stuinfo; -- Gitee