From 1ef5872f8f9bb8f380e132cff671cdbd7c96f280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=97=BB=E7=A5=A5?= <18396581747@163.com> Date: Wed, 1 Mar 2023 07:43:01 +0000 Subject: [PATCH 1/3] =?UTF-8?q?21=20=E5=91=A8=E9=97=BB=E7=A5=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周闻祥 <18396581747@163.com> --- ...60\343\200\201\344\275\234\344\270\232.md" | 252 ++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 "21 \345\221\250\351\227\273\347\245\245/20230227 \345\205\263\350\201\224\346\237\245\350\257\242\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" diff --git "a/21 \345\221\250\351\227\273\347\245\245/20230227 \345\205\263\350\201\224\346\237\245\350\257\242\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" "b/21 \345\221\250\351\227\273\347\245\245/20230227 \345\205\263\350\201\224\346\237\245\350\257\242\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" new file mode 100644 index 0000000..1590bc2 --- /dev/null +++ "b/21 \345\221\250\351\227\273\347\245\245/20230227 \345\205\263\350\201\224\346\237\245\350\257\242\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" @@ -0,0 +1,252 @@ +# 笔记 + +```mysql +内连接:SELECT * FROM 表A inner join 表B on A的字段=B的字段 where 条件 +左连接:... 表A left join 表B on A的字段=B的字段 ... +右连接:... 表A right join 表B on A的字段=B的字段 ... +合并连接:.... 表A right join 表B on A的字段=B的字段 .... + union + .... 表A right join 表B on A的字段=B的字段 .... +``` + +# 作业 + +```mysql +-- 1. 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。 +-- 2. 数据库的表结构 +-- 表(一)Student (学生表) +-- 属性名 数据类型 可否为空 含义 +-- Sno varchar (20) 否 学号(主码) +-- Sname varchar (20) 否 学生姓名 +-- Ssex varchar (20) 否 学生性别 +-- Sbirthday datetime 可 学生出生年月 +-- Class varchar (20) 可 学生所在班级 +create database stu charset utf8; +use stu; + +create table student( + sno varchar(20)primary key, + sname varchar(20)not null, + ssex varchar(20)not null, + sbirthday datetime, + class varchar(20) +); +-- 表(二)Course(课程表) +-- 属性名 数据类型 可否为空 含义 +-- Cno varchar (20) 否 课程号(主码) +-- Cname varchar (20) 否 课程名称 +-- Tno varchar (20) 否 教工编号(外码) +create table course( + cno varchar(20)primary key, + cname varchar(20) not null, + tno varchar(20)not null +); +ALTER table course add FOREIGN key (tno) REFERENCES teacher(tno) on UPDATE CASCADE; +-- 表(三)Score(成绩表) +-- 属性名 数据类型 可否为空 含义 +-- Sno varchar (20) 否 学号(外码) +-- Cno varchar (20) 否 课程号(外码) +-- Degree Decimal(4,1) 可 成绩 +-- 主码:Sno+ Cno +create table score( + sno varchar(20)not null, + cno varchar(20)not null, + degree decimal(4,1) +); +ALTER table score add FOREIGN key (sno) REFERENCES student(sno) on UPDATE CASCADE; +ALTER table score add FOREIGN key (cno) REFERENCES course(cno) on UPDATE CASCADE; +-- 表(四)Teacher(教师表) +-- 属性名 数据类型 可否为空 含义 +-- Tno varchar (20) 否 教工编号(主码) +-- Tname varchar (20) 否 教工姓名 +-- Tsex varchar (20) 否 教工性别 +-- Tbirthday datetime 可 教工出生年月 +-- Prof varchar (20) 可 职称 +-- Depart varchar (20) 否 教工所在部门 +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 +); +-- 3. 数据库中的数据: +-- -- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ +use stu; +select * from student + left join score on student.sno=score.Sno + left join course on score.Cno=course.cno ORDER BY sname; +-- -- 2,查询没有学生的教师的所有信息 +select * from teacher + left join course on teacher.tno=course.tno + left join score on course.cno=score.Cno where sno <=> null; + +-- +-- 表(一)Student +-- Sno Sname Ssex Sbirthday class +-- 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 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'); +-- 表(二)Course +-- Cno Cname Tno +-- 3-105 计算机导论 825 +-- 3-245 操作系统 804 +-- 6-166 数字电路 856 +-- 9-888 高等数学 831 +INSERT INTO course values +('3-105','计算机导论','825'), +('3-245','操作系统','804'), +('6-166','数字电路','856'), +('9-888','高等数学','831'); +-- 表(三)Score +-- Sno Cno Degree +-- 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 +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'); + +-- 表(四)Teacher +-- Tno Tname Tsex Tbirthday Prof Depart +-- 804 李诚 男 1958-12-2 副教授 计算机系 +-- 856 张旭 男 1969-3-12 讲师 电子工程系 +-- 825 王萍 女 1972-5-5 助教 计算机系 +-- 831 刘冰 女 1977-8-14 助教 电子工程系 +insert into teacher VALUES +('804','李诚','男','1958-12-2','副教授','计算机系'), +('856','张旭','男','1969-3-12','讲师','电子工程系'), +('825','王萍','女','1972-5-5','助教','计算机系'), +('831','刘冰','女','1977-8-14','助教','电子工程系'); +-- 4.查询 +-- ① 查询Score表中的最高分的学生学号和课程号。 +select sno,cno from score where degree=(select max(degree) from score); +-- ② 查询所有学生的Sname、Cno和Degree列。 +select sname,score.cno,degree from student left join score on student.sno=score.sno; +-- ③ 查询所有学生的Sno、Cname和Degree列。 +select score.sno,cname,degree from score left join course on score.cno=course.cno; +-- ④ 查询所有学生的Sname、Cname和Degree列。 +select sname,cname,degree from student + left join score on student.sno=score.sno + left join course on score.cno=course.cno; +-- ⑤ 查询“95033”班学生的平均分。 +select avg(score.Degree) from score left 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.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 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 degree +-- ⑰ 查询所有任课教师的Tname和Depart. +select tname,depart from teacher +-- ⑱ 查询所有未讲课的教师的Tname和Depart. +select tname,depart from teacher +-- ⑲ 查询“男”教师及其所上的课程。 +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 e193085e6bfaffbe385fa75ba192e107c221e0ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=97=BB=E7=A5=A5?= <18396581747@163.com> Date: Mon, 6 Mar 2023 11:40:59 +0000 Subject: [PATCH 2/3] =?UTF-8?q?21=20=E5=91=A8=E9=97=BB=E7=A5=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周闻祥 <18396581747@163.com> --- ...60\343\200\201\344\275\234\344\270\232.md" | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 "21 \345\221\250\351\227\273\347\245\245/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" diff --git "a/21 \345\221\250\351\227\273\347\245\245/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" "b/21 \345\221\250\351\227\273\347\245\245/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" new file mode 100644 index 0000000..481c7de --- /dev/null +++ "b/21 \345\221\250\351\227\273\347\245\245/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" @@ -0,0 +1,116 @@ +# 笔记 + +```mysql +子查询放在where,having后面 +单列单值直接用>,<,=等运算符 +单列多值用in,not in或搭配any使用 +子查询3种用法:select 子查询1 from 子查询2 where 子查询3 +``` + +# 作业 + +```mysql +-- 在如图的数据表上完成以下题目 +create database stu1 CHARSET utf8; +use stu1; +CREATE table stuinfo( +stuno VARCHAR(20) PRIMARY key, +stuname VARCHAR(20), +stusex enum('男','女'), +stuage int, +stuaddress VARCHAR(20), +stuseat int unique key auto_increment); + +CREATE TABLE stuexam( +examno int PRIMARY key auto_increment, +stuno VARCHAR(20), +writtenexam int, +labexam int); + +CREATE TABLE stumarks( +examno int PRIMARY key auto_increment, +stuid VARCHAR(20), +score int); + +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 stuno,stuname,stusex,score from stuinfo left join stumarks on stuinfo.stuno=stumarks.stuid where score in +(SELECT max(score) from stumarks GROUP BY stuid)and stuno=stuid +UNION +select stuno,stuname,stusex,score from stuinfo left join stumarks on stuinfo.stuno=stumarks.stuid where score is null; +-- +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +SELECT examno,stuname,stusex,avg(cj) 平均分 from +(select examno,stuno,writtenexam cj from stuexam +UNION +select examno,stuno,labexam from stuexam) as pjcj +left join stuinfo on pjcj.stuno=stuinfo.stuno +GROUP BY examno; +-- +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +SELECT * from stuinfo where stuage>=20 and stusex='男'; +SELECT * from (SELECT * from stuinfo where stuage>=20 and stusex='男') as acx; +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stuage>(SELECT max(stuage) from stuinfo where stusex='男') and stusex='女'; +-- +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +SELECT * from stumarks left join stuinfo on stumarks.stuid=stuinfo.stuno where stuid != any(select stuid from stumarks where score<60); +-- +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select stuinfo.* from stumarks left join stuinfo on stumarks.stuid=stuinfo.stuno GROUP BY stuno; +SELECT * from stuinfo where stuno in (SELECT stuid from stumarks); +-- +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select stuinfo.* from stumarks right join stuinfo on stumarks.stuid=stuinfo.stuno where score is null GROUP BY stuno; +SELECT * from stuinfo where stuno not in (SELECT stuid from stumarks); +-- +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +SELECT stuinfo.* from stumarks left join stuinfo on stumarks.stuid=stuinfo.stuno where stuid = any(select stuid from stumarks where score>90) GROUP BY stuid; +-- +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select * from stuinfo where stuno in +(SELECT stuid from stumarks WHERE score>80); +-- +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select stuinfo.* from stuinfo inner join stumarks on stumarks.stuid=stuinfo.stuno where score>( +select max(score) from stuinfo inner join stumarks on stumarks.stuid=stuinfo.stuno where stuname ='张秋利') GROUP BY stuname; +-- +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select stuinfo.* from stuinfo inner join stumarks on stumarks.stuid=stuinfo.stuno where score> any( +select score from stuinfo inner join stumarks on stumarks.stuid=stuinfo.stuno where stuname ='张秋利') and stuname != '张秋利' GROUP BY stuname; +-- +-- 13.查询班上比所有男生年龄都要大的女生的信息 +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 From 978de4e66434253b1fc33e6716a032e9a15fb935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=97=BB=E7=A5=A5?= <18396581747@163.com> Date: Mon, 6 Mar 2023 11:41:26 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=2021=20?= =?UTF-8?q?=E5=91=A8=E9=97=BB=E7=A5=A5/20230306=20=20=E5=AD=90=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=9A=84=E7=AC=94=E8=AE=B0=E3=80=81=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?.md=20=E4=B8=BA=2021=20=E5=91=A8=E9=97=BB=E7=A5=A5/20230306=20?= =?UTF-8?q?=20=E5=AD=90=E6=9F=A5=E8=AF=A2=E7=AC=94=E8=AE=B0=E3=80=81?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "21 \345\221\250\351\227\273\347\245\245/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" => "21 \345\221\250\351\227\273\347\245\245/20230306 \345\255\220\346\237\245\350\257\242\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" (100%) diff --git "a/21 \345\221\250\351\227\273\347\245\245/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" "b/21 \345\221\250\351\227\273\347\245\245/20230306 \345\255\220\346\237\245\350\257\242\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" similarity index 100% rename from "21 \345\221\250\351\227\273\347\245\245/20230306 \345\255\220\346\237\245\350\257\242\347\232\204\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" rename to "21 \345\221\250\351\227\273\347\245\245/20230306 \345\255\220\346\237\245\350\257\242\347\254\224\350\256\260\343\200\201\344\275\234\344\270\232.md" -- Gitee