From 4780324f3be44e1e8582e00a9d64a894d6416634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=99=9F=E8=BE=89?= <2081404735@qq.com> Date: Wed, 1 Mar 2023 12:31:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=81=94=E5=90=88=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\350\257\242\347\254\224\350\256\260.md" | 266 ++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 "06 \346\236\227\346\231\237\350\276\211/20230227 \350\201\224\345\220\210\346\237\245\350\257\242\347\254\224\350\256\260.md" diff --git "a/06 \346\236\227\346\231\237\350\276\211/20230227 \350\201\224\345\220\210\346\237\245\350\257\242\347\254\224\350\256\260.md" "b/06 \346\236\227\346\231\237\350\276\211/20230227 \350\201\224\345\220\210\346\237\245\350\257\242\347\254\224\350\256\260.md" new file mode 100644 index 0000000..195f3ec --- /dev/null +++ "b/06 \346\236\227\346\231\237\350\276\211/20230227 \350\201\224\345\220\210\346\237\245\350\257\242\347\254\224\350\256\260.md" @@ -0,0 +1,266 @@ +# 一、笔记 + +```sql +-- 1.内连接 A表和B表共有的 +SELECT 字段列表 FROM A表 INNER JOIN B表; +-- 2.左连接 A表全部 +SELECT 字段列表 FROM A表 LEFT JOIN B表; +-- 3.右连接 B表全部 +SELECT 字段列表 FROM A表 RIGHT JOIN B表; +-- 4全外连接 A表全部和B表全部 +SELECT 字段列表 FROM A表 LEFT JOIN B表 +ON A表.两表都有的列 = B表.两表都有的列 +UNION +SELECT 字段列表 FROM A表 RIGHT JOIN B表 +ON A表.两表都有的列 = B表.两表都有的列; +-- 5全外连接 除去A表和B表共有的 +SELECT 字段列表 FROM A表 LEFT JOIN B表 +ON A表.两表都有的列 = B表.两表都有的列 +WHERE 条件 +UNION +SELECT 字段列表 FROM A表 RIGHT JOIN B表 +ON A表.两表都有的列 = B表.两表都有的列 +WHERE 条件; +``` + + + +# 二、作业 + +1. 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。 +-- + +```sql +create database yyg charset utf8; +``` + +2. **数据库的表结构** + +```sql +use yyg; +``` + + +表(一)Student (学生表) +-- +| 属性名 | 数据类型 | 可否为空 | 含义 | +| --------- | ------------ | -------- | ------------ | +| Sno | varchar (20) | 否 | 学号(主码) | +| Sname | varchar (20) | 否 | 学生姓名 | +| Ssex | varchar (20) | 否 | 学生性别 | +| Sbirthday | datetime | 可 | 学生出生年月 | +| Class | varchar (20) | 可 | 学生所在班级 | +```sql +create table Student( + Sno varchar(20) primary key comment"学号", + Sname varchar(20) not null comment"学生姓名", + Ssex varchar(20) not null comment"学生性别", + Sbirthday datetime comment'学生出生年月', + Class varchar(20) comment'学生所在班级' +); +``` +表(二)Course(课程表) +-- +| 属性名 | 数据类型 | 可否为空 | 含义 | +| ------ | ------------ | -------- | ---------------- | +| Cno | varchar (20) | 否 | 课程号(主码) | +| Cname | varchar (20) | 否 | 课程名称 | +| Tno | varchar (20) | 否 | 教工编号(外码) | +| | | | | +```sql +create table Course( + Cno varchar (20) primary key comment'课程号', + Cname varchar(20) not null comment'课程名称 ', + Tno varchar(20) not null comment'教工编号', + foreign key (Tno) references Teacher(Tno) +); +``` +表(三)Score(成绩表) +-- +| 属性名 | 数据类型 | 可否为空 | 含义 | +| -------------- | ------------ | -------- | -------------- | +| Sno | varchar (20) | 否 | 学号(外码) | +| Cno | varchar (20) | 否 | 课程号(外码) | +| Degree | Decimal(4,1) | 可 | 成绩 | +| 主码:Sno+ Cno | | | | +```sql +create table Score( + Sno varchar(20) not null comment'学号', + Cno varchar(20) not null comment'课程号', + Degree Decimal(4,1) comment'成绩', + foreign key (Sno) references Student(Sno), + foreign key (Cno) references Course(Cno), + primary key(Sno,Cno) +); +``` +表(四)Teacher(教师表) +-- +| 属性名 | 数据类型 | 可否为空 | 含义 | +| --------- | ------------ | -------- | ---------------- | +| Tno | varchar (20) | 否 | 教工编号(主码) | +| Tname | varchar (20) | 否 | 教工姓名 | +| Tsex | varchar (20) | 否 | 教工性别 | +| Tbirthday | datetime | 可 | 教工出生年月 | +| Prof | varchar (20) | 可 | 职称 | +| Depart | varchar (20) | 否 | 教工所在部门 | +```sql +create table Teacher( + Tno varchar(20) primary key not null comment'教工编号', + Tname varchar(20)not null comment'教工姓名', + Tsex varchar(20)not null comment'教工性别', + Tbirthday datetime comment'教工出生年月', + Prof varchar(20) comment'职称', + Depart varchar(20)not null comment'教工所在部门' +); +``` +表(一)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 | +```sql +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 | +```sql +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 | +```sql +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 | +| ---- | ----- | ---- | --------- | ------ | +| 804 | 李诚 | 男 | 1958-12-2 | 副教授 | +| 856 | 张旭 | 男 | 1969-3-12 | 讲师 | +| 825 | 王萍 | 女 | 1972-5-5 | 助教 | +| 831 | 刘冰 | 女 | 1977-8-14 | 助教 | +```sql +insert into Teacher values + (804,'李诚','男','1958-12-2','副教授','计算机系'), + (856,'张旭','男','1969-3-12','讲师','电子工程系'), + (825,'王萍','女','1972-5-5','助教','计算机系'), + (831,'刘冰','女','1977-8-14','助教','电子工程系'); +``` +3. **数据库中的数据**: +-- +1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ + +```sql +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,查询没有学生的教师的所有信息 + +```sql +select Teacher.Tno,Tname,Tsex,Tbirthday,Prof,Depart +from Teacher left join Course on Teacher.Tno = Course.Tno +left join Score on Course.Cno = Score.Cno +where Score.Sno is null; +``` + + +4. 查询 +-- +```sql +-- 1 查询Score表中的最高分的学生学号和课程号。 +select Sno 学号,Cno 课程号 from Score +where Degree = (select max(Degree) from Score); +-- 2 查询所有学生的Sname、Cno和Degree列。 +select Sname,Cno,Degree +from Student inner join Score on Student.Sno = Score.Sno; +-- 3 查询所有学生的Sno、Cname和Degree列。 +select Sno,Cname,Degree from Score inner join Course on Score.Cno = Course.Cno; +-- 4 查询所有学生的Sname、Cname和Degree列。 +select Sname,Cname,Degree from Student,Course,Score where Student.Sno=Score.Sno and Course.Cno=Score.Cno; +-- 5 查询“95033”班学生的平均分。 +select avg(Degree) from Score inner join Student on Score.Sno=Student.Sno where class=95033; +-- 6 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select * from Score where Degree>(select Degree from Score where Sno = '109' and Cno = '3-105' ) and Cno = '3-105'; +-- 7 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select * from Score a where Degree <(select MAX(degree) from Score b where a.Cno=b.Cno) and Sno in(select Sno from Score group by Sno having count(*)>1); +-- 8 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from Score where Degree >(select Degree from Score where Sno='109' and Cno='3-105'); +-- 9 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select Sno,Sname,Sbirthday from Student where Sbirthday=(select Sbirthday from Student where Sno='108'); +-- 10 查询“张旭“教师任课的学生成绩。 +-select Sno,Degree from Score where Cno=(select Cno from Course inner join Teacher ON Course.Tno= Teacher.Tno where Tname='张旭' ); +-- 11 查询选修某课程的同学人数多于5人的教师姓名。 +select Tname from Teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having COUNT(Cno)>5)); +-- 12 查询出“计算机系“教师所教课程的成绩表。 +select sno,Cno ,Degree from Score where Cno in +(select Cno from Course where Tno IN (select Tno from Teacher where Depart='计算机系')); +-- 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select Tname from Teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having COUNT(Cno)>5)); +-- 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select max(Degree) from Score where Cno= '3-245'); +-- 15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select max(Degree) from Score where Cno= '3-245'); +-- 16 查询成绩比该课程平均成绩低的同学的成绩表。 +select Cno,Sno,Degree from Score where Degree > avg(sum(select Degree from Score where Cno='3-245')); +-- 17 查询所有任课教师的Tname和Depart. +select Tname,Depart from Teacher; +-- 18 查询所有未讲课的教师的Tname和Depart. +select Tname,Depart from Teacher left join Course on Teacher.Tno=Course.Tno left join Score on Course.Cno=Score.Cno where Score.Cno is null; +-- 19 查询“男”教师及其所上的课程。 +select Tname,Depart from Teacher where Tsex = '男'; +-- 20 查询最高分同学的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 * from Score where Sno in (select Sno from Student where Ssex ='男') +and Cno = (select Cno from Course where Cname='计算机导论'); +``` \ No newline at end of file -- Gitee From e9faa3ca29fc42292420401e26442c03ee1ba134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=99=9F=E8=BE=89?= <2081404735@qq.com> Date: Wed, 1 Mar 2023 21:52:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AD=90=E6=9F=A5=E8=AF=A2=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\350\257\242\347\254\224\350\256\260.md" | 265 ++++++++++++++---- ...45\350\257\242\347\254\224\350\256\260.md" | 25 ++ 2 files changed, 235 insertions(+), 55 deletions(-) create mode 100644 "06 \346\236\227\346\231\237\350\276\211/20230301\345\255\220\346\237\245\350\257\242\347\254\224\350\256\260.md" diff --git "a/06 \346\236\227\346\231\237\350\276\211/20230227 \350\201\224\345\220\210\346\237\245\350\257\242\347\254\224\350\256\260.md" "b/06 \346\236\227\346\231\237\350\276\211/20230227 \350\201\224\345\220\210\346\237\245\350\257\242\347\254\224\350\256\260.md" index 195f3ec..cec8393 100644 --- "a/06 \346\236\227\346\231\237\350\276\211/20230227 \350\201\224\345\220\210\346\237\245\350\257\242\347\254\224\350\256\260.md" +++ "b/06 \346\236\227\346\231\237\350\276\211/20230227 \350\201\224\345\220\210\346\237\245\350\257\242\347\254\224\350\256\260.md" @@ -40,9 +40,8 @@ create database yyg charset utf8; use yyg; ``` - 表(一)Student (学生表) --- + | 属性名 | 数据类型 | 可否为空 | 含义 | | --------- | ------------ | -------- | ------------ | | Sno | varchar (20) | 否 | 学号(主码) | @@ -60,7 +59,7 @@ create table Student( ); ``` 表(二)Course(课程表) --- + | 属性名 | 数据类型 | 可否为空 | 含义 | | ------ | ------------ | -------- | ---------------- | | Cno | varchar (20) | 否 | 课程号(主码) | @@ -76,7 +75,7 @@ create table Course( ); ``` 表(三)Score(成绩表) --- + | 属性名 | 数据类型 | 可否为空 | 含义 | | -------------- | ------------ | -------- | -------------- | | Sno | varchar (20) | 否 | 学号(外码) | @@ -94,7 +93,7 @@ create table Score( ); ``` 表(四)Teacher(教师表) --- + | 属性名 | 数据类型 | 可否为空 | 含义 | | --------- | ------------ | -------- | ---------------- | | Tno | varchar (20) | 否 | 教工编号(主码) | @@ -114,7 +113,7 @@ create table Teacher( ); ``` 表(一)Student --- + | Sno | Sname | Ssex | Sbirthday | class | | ---- | ----- | ---- | --------- | ----- | | 108 | 曾华 | 男 | 1977-9-1 | 95033 | @@ -133,7 +132,7 @@ insert into Student values ('103','陆君','男','1974-6-3','95031'); ``` 表(二)Course --- + | Cno | Cname | Tno | | ----- | ---------- | ---- | | 3-105 | 计算机导论 | 825 | @@ -149,7 +148,7 @@ insert into Course values ``` 表(三)Score --- + | Sno | Cno | Degree | | ---- | ----- | ------ | | 103 | 3-245 | 86 | @@ -176,7 +175,7 @@ insert into Score values ('108','6-166','81'); ``` 表(四)Teacher --- + | Tno | Tname | Tsex | Tbirthday | Prof | | ---- | ----- | ---- | --------- | ------ | | 804 | 李诚 | 男 | 1958-12-2 | 副教授 | @@ -190,77 +189,233 @@ insert into Teacher values (825,'王萍','女','1972-5-5','助教','计算机系'), (831,'刘冰','女','1977-8-14','助教','电子工程系'); ``` -3. **数据库中的数据**: +3. 查询 -- -1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ ```sql -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,查询没有学生的教师的所有信息 - -```sql -select Teacher.Tno,Tname,Tsex,Tbirthday,Prof,Depart -from Teacher left join Course on Teacher.Tno = Course.Tno -left join Score on Course.Cno = Score.Cno -where Score.Sno is null; -``` +-- 3. 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ - -4. 查询 --- -```sql +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 + Teacher.Tno, + Tname, + Tsex, + Tbirthday, + Prof, + Depart +FROM + Teacher + LEFT JOIN Course ON Teacher.Tno = Course.Tno + LEFT JOIN Score ON Course.Cno = Score.Cno +WHERE + Score.Sno IS NULL; + -- 1 查询Score表中的最高分的学生学号和课程号。 -select Sno 学号,Cno 课程号 from Score -where Degree = (select max(Degree) from Score); + +SELECT + Sno 学号, + Cno 课程号 +FROM + Score +WHERE + Degree = ( SELECT max( Degree ) FROM Score ); + -- 2 查询所有学生的Sname、Cno和Degree列。 -select Sname,Cno,Degree -from Student inner join Score on Student.Sno = Score.Sno; + +SELECT + Sname, + Cno, + Degree +FROM + Student + INNER JOIN Score ON Student.Sno = Score.Sno; + -- 3 查询所有学生的Sno、Cname和Degree列。 -select Sno,Cname,Degree from Score inner join Course on Score.Cno = Course.Cno; + +SELECT + Sno, + Cname, + Degree +FROM + Score + INNER JOIN Course ON Score.Cno = Course.Cno; + -- 4 查询所有学生的Sname、Cname和Degree列。 -select Sname,Cname,Degree from Student,Course,Score where Student.Sno=Score.Sno and Course.Cno=Score.Cno; + +SELECT + Sname, + Cname, + Degree +FROM + Student, + Course, + Score +WHERE + Student.Sno = Score.Sno + AND Course.Cno = Score.Cno; + -- 5 查询“95033”班学生的平均分。 -select avg(Degree) from Score inner join Student on Score.Sno=Student.Sno where class=95033; + +SELECT + class, + avg( Degree ) +FROM + Score + INNER JOIN Student ON Score.Sno = Student.Sno +WHERE + class = 95033; + -- 6 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 -select * from Score where Degree>(select Degree from Score where Sno = '109' and Cno = '3-105' ) and Cno = '3-105'; + +SELECT * FROM Score WHERE Degree >( SELECT Degree FROM Score WHERE Sno = '109' AND Cno = '3-105' ) +AND Cno = '3-105'; + -- 7 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 -select * from Score a where Degree <(select MAX(degree) from Score b where a.Cno=b.Cno) and Sno in(select Sno from Score group by Sno having count(*)>1); + +SELECT * FROM Score a WHERE Degree <(SELECT MAX( degree ) FROM Score b WHERE a.Cno = b.Cno ) +AND Sno IN ( SELECT Sno FROM Score GROUP BY Sno HAVING count(*)> 1 ); + -- 8 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 -select * from Score where Degree >(select Degree from Score where Sno='109' and Cno='3-105'); + +SELECT * FROM Score WHERE Degree >( SELECT Degree FROM Score WHERE Sno = '109' AND Cno = '3-105' ); + -- 9 查询和学号为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' ); + -- 10 查询“张旭“教师任课的学生成绩。 --select Sno,Degree from Score where Cno=(select Cno from Course inner join Teacher ON Course.Tno= Teacher.Tno where Tname='张旭' ); + +SELECT + Sno,Degree +FROM + Score +WHERE + Cno =( + SELECT + Cno + FROM + Course + INNER JOIN Teacher ON Course.Tno = Teacher.Tno + WHERE + Tname = '张旭' + ); + -- 11 查询选修某课程的同学人数多于5人的教师姓名。 -select Tname from Teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having COUNT(Cno)>5)); + +SELECT Tname FROM Teacher WHERE Tno =( SELECT Tno FROM Course WHERE Cno =( SELECT Cno FROM Score GROUP BY Cno HAVING COUNT( Cno )> 5 )); + -- 12 查询出“计算机系“教师所教课程的成绩表。 -select sno,Cno ,Degree from Score where Cno in -(select Cno from Course where Tno IN (select Tno from Teacher where Depart='计算机系')); + +SELECT + sno,Cno,Degree +FROM + Score +WHERE + Cno IN ( + SELECT + Cno + FROM + Course + WHERE + Tno IN ( SELECT Tno FROM Teacher WHERE Depart = '计算机系' )); + -- 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 -select Tname from Teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having COUNT(Cno)>5)); + +SELECT + Tname,prof +FROM + Teacher +WHERE prof in (SELECT Prof FROM Teacher WHERE Teacher.Depart = '计算机系' ) xor prof in ( SELECT Prof FROM Teacher WHERE Teacher.Depart = '电子工程系' ); + -- 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 -select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select max(Degree) from Score where Cno= '3-245'); + +SELECT Cno, Sno, Degree FROM Score WHERE Cno = '3-105' AND Degree >( SELECT max( Degree ) FROM Score WHERE Cno = '3-245' ); + -- 15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. -select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select max(Degree) from Score where Cno= '3-245'); + +SELECT Cno, Sno, Degree FROM Score WHERE Cno = '3-105' AND Degree >( SELECT max( Degree ) FROM Score WHERE Cno = '3-245' ); + -- 16 查询成绩比该课程平均成绩低的同学的成绩表。 -select Cno,Sno,Degree from Score where Degree > avg(sum(select Degree from Score where Cno='3-245')); + +SELECT + * +FROM + Score a +WHERE + Degree < ( SELECT avg( Degree ) FROM score b WHERE a.Cno = b.Cno ); + -- 17 查询所有任课教师的Tname和Depart. -select Tname,Depart from Teacher; + +SELECT + Tname,Depart +FROM + Teacher; + -- 18 查询所有未讲课的教师的Tname和Depart. -select Tname,Depart from Teacher left join Course on Teacher.Tno=Course.Tno left join Score on Course.Cno=Score.Cno where Score.Cno is null; + +SELECT + Tname, + Depart +FROM + Teacher + LEFT JOIN Course ON Teacher.Tno = Course.Tno + LEFT JOIN Score ON Course.Cno = Score.Cno +WHERE + Score.Cno IS NULL; + -- 19 查询“男”教师及其所上的课程。 -select Tname,Depart from Teacher where Tsex = '男'; + +SELECT + Tname,Depart +FROM + Teacher +WHERE + Tsex = '男'; + -- 20 查询最高分同学的Sno、Cno和Degree列。 -select Sno,Cno,degree from Score where Degree = (select max(Degree) from Score); + +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 ='李军'); + +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 ='李军') + +SELECT Sname FROM Student WHERE Ssex =( SELECT Ssex FROM Student WHERE Sname = '李军' ) +AND class =( + SELECT + class + FROM + Student + WHERE + Sname = '李军') + -- 23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 -Select * from Score where Sno in (select Sno from Student where Ssex ='男') -and Cno = (select Cno from Course where Cname='计算机导论'); + +SELECT + * +FROM + Score +WHERE + Sno IN ( SELECT Sno FROM Student WHERE Ssex = '男' ) + AND Cno = ( SELECT Cno FROM Course WHERE Cname = '计算机导论' ); ``` \ No newline at end of file diff --git "a/06 \346\236\227\346\231\237\350\276\211/20230301\345\255\220\346\237\245\350\257\242\347\254\224\350\256\260.md" "b/06 \346\236\227\346\231\237\350\276\211/20230301\345\255\220\346\237\245\350\257\242\347\254\224\350\256\260.md" new file mode 100644 index 0000000..494715c --- /dev/null +++ "b/06 \346\236\227\346\231\237\350\276\211/20230301\345\255\220\346\237\245\350\257\242\347\254\224\350\256\260.md" @@ -0,0 +1,25 @@ +# 一、笔记 + +## 1、select中嵌套子查询 + +​ 嵌套在另一个SQL语句中的查询,select语句可以嵌套在另一个select中,update,delete,insert,create等语句也可以,通常使用与where或having中 + +## 2、 SELECT的WHERE或HAVING中嵌套子查询 + +​ 如果子查询作为另一个SQL的过滤条件,通常把子查询嵌入到where或having中。 + +根据子查询结果不同可以分为三种情况 + +- 如果子查询是单列单值可以用比较运算符进行比较 +- 如果子查询是单列多值可以用in或者not in进行比较 +- 如果子查询是多列多值还可以用比较运算符搭配any、some、all等进行查询 + +## 3、select中嵌套exists型子查询 + +​ exists型子查询是可以存在外层select的where子句中,不过它和上面的where型子查询的工作模式不一样 + +​ 如果exists关键字后面的参数是任意一个子查询,系统会对子查询进行运算用来判断他要不要返回行,如果至少返回一行,那么exists的结果位true,那么外层查询语句进行查询,反之如果没有返回行,那么exists结果为false,外层查询语句不进行查询,exists和not exists的结果只取决于是否返回行不取决于这些行的内容。 + +​ 如果exists关键字后面的参数是一个关联子查询,那么子查询的where条件中包含与外层查询表的关联条件,那么此时将对外层查询表做循环,即在筛选外层查询表的每一条记录都看是否满足子查询的条件,如果满足那么就用外层进行二次筛选,否则丢弃这行记录。 + +​ \ No newline at end of file -- Gitee