diff --git "a/34 \351\230\216\345\206\240\345\256\207/20230226 \345\244\232\350\241\250\350\201\224\345\220\210\346\237\245\350\257\242\347\232\204\351\242\204\344\271\240.md" "b/34 \351\230\216\345\206\240\345\256\207/20230226 \345\244\232\350\241\250\350\201\224\345\220\210\346\237\245\350\257\242\347\232\204\351\242\204\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..bd813f185741b6a0fe7a2ebc8657a2af4240d6c0 --- /dev/null +++ "b/34 \351\230\216\345\206\240\345\256\207/20230226 \345\244\232\350\241\250\350\201\224\345\220\210\346\237\245\350\257\242\347\232\204\351\242\204\344\271\240.md" @@ -0,0 +1,45 @@ +# MySQL多表联合查询的预习 + +### 介绍 + +多表查询就是同时查询两个或两个以上的表,因为有的时候用户在查看数据的时候,需要显示的数据来自多张表。外键约束对多表查询并无影响。 + +### 分类 + +##### 交叉连接查询 (产生笛卡尔积,了解) + +```mysql +语法:select * from A,B; +``` + +##### 内连接查询(使用的关键字inner join --inner可以省略) + +```sql +隐式内连接 : select * from A,B where 条件; + +显式内连接 : select * from A inner join B on 条件; +``` + +##### 外连接查询 (使用的关键字 outer join --outer 可以省略) + +```mysql +左外连接 :left outer join + + select * from A left outer join B on 条件; + +右外连接 :right outer join + + select * from A right outer join B on 条件; + +满外连接 :full outer join + + select * from A full outer join B on 条件; +``` + +##### 子查询 + +select 的嵌套 + +##### 表自关联: + +将一张表当成多张表来用 \ No newline at end of file diff --git "a/34 \351\230\216\345\206\240\345\256\207/20230227 \345\210\206\347\273\204\345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242\347\232\204\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" "b/34 \351\230\216\345\206\240\345\256\207/20230227 \345\210\206\347\273\204\345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242\347\232\204\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..6b0094089061a4ac31d9aec2044dc89096cc36b3 --- /dev/null +++ "b/34 \351\230\216\345\206\240\345\256\207/20230227 \345\210\206\347\273\204\345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242\347\232\204\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" @@ -0,0 +1,215 @@ +# 笔记 + +### 介绍 + +多表查询就是同时查询两个或两个以上的表,因为有的时候用户在查看数据的时候,需要显示的数据来自多张表。外键约束对多表查询并无影响。 + +### 分类 + +##### 交叉连接查询 (产生笛卡尔积,了解) + +```mysql +语法:select * from A,B; +``` + +##### 内连接查询(使用的关键字inner join --inner可以省略) + +```sql +隐式内连接 : select * from A,B where 条件; + +显式内连接 : select * from A inner join B on 条件; +``` + +##### 外连接查询 (使用的关键字 outer join --outer 可以省略) + +```mysql +左外连接 :left outer join + + select * from A left outer join B on 条件; + +右外连接 :right outer join + + select * from A right outer join B on 条件; + +满外连接 :full outer join + + select * from A full outer join B on 条件; +``` + +##### 子查询 + +select 的嵌套 + +##### 表自关联: + +将一张表当成多张表来用 + +### 函数 + +###### 分组函数 + +```mysql +AVG(x) :求平均值 +SUM(x):求总和 +MAX(x):求最大值 +MIN(x):求最小值 +COUNT(x):统计记录数 +``` + + + +###### 日期时间函数 + +```mysql +CURDATE() 当前日期 +CURTIME() 当前时间 +NOW() 当前日期+时间 +year() 取年 +month() 取月 +day() 取天 +DATEDIFF(date1,date2)/TIMEDIFF(time1,time2) 返回两个时间的间隔 + +###### 条件判断 +``` + + + +```mysql +(1)IF(value,t,f):如果value是true,那么整个表达式的结果是t,否则就是f +(2)IFNULL(value1,value2) +(3) +CASE WHEN 条件1 THEN result1 + WHEN 条件2 THEN result2 … + ELSE resultn +END 相当于JAVA中if..else if... + +(4) +CASE expr +WHEN 常量值1 THEN 值1 +WHEN 常量值2 THEN 值2 … +ELSE 值n +END +``` + + + +# 作业 + +```mysql +create database class6 charset utf8; +use class6; +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#教工编号(外码) +); +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,#教工编号(主码) + 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); +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); +insert into Teacher VALUES +(804,'李诚','男','1958-12-2','副教授','计算机系'), +(856,'张旭','男','1969-3-12','讲师','电子工程系'), +(825,'王萍','女','1972-5-5','助教','计算机系'), +(831,'刘冰','女','1977-8-14','助教','电子工程系'); +-- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息 +select + * + from Student,Course,Score +where Student.Sno=Score.Sno AND Course.Cno=Score.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 in (select MAX(Degree) from Score); +#② 查询所有学生的Sname、Cno和Degree列。 +select Sname,Cno,Degree from Student,Score where Student.Sno=Score.Sno; +#③ 查询所有学生的Sno、Cname和Degree列。 +select Sno,Cname,Degree from Course,Score where Course.Cno=Score.Cno; +#④ 查询所有学生的Sname、Cname和Degree列。 +select Sname,Cname,Degree from Student,Course,Score where Student.Sno=Score.Sno AND Course.Cno=Score.Cno; +#⑤ 查询“95033”班学生的平均分。 +select AVG(Degree) from Student,Score where Student.Sno=Score.Sno AND Class=95033; +#⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select * from Student,Score where Student.Sno=Score.Sno and Score.Degree>(select Degree from Score where Cno='3-105' and Sno='109'); +#⑦ 查询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); +#⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from Student,Score where Student.Sno=Score.Sno and Score.Degree>(select Degree from Score where Cno='3-105' and Sno='109'); +#⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select Sno,Sname,Sbirthday from Student where year(student.Sbirthday)=(select year(Sbirthday) from student where Sno='108'); +#⑩ 查询“张旭“教师任课的学生成绩。 +select Sname,Degree from Student,Score where Student.Sno=Score.Sno and Cno ='6-166'; +#11 查询选修某课程的同学人数多于5人的教师姓名。 +select Tname from Teacher where Tno in (select Tno from Course where Cno in (select Cno from Score group by Cno having COUNT(*)>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,Prof from Teacher a where Prof not in(select Prof from Teacher b where a.Depart!=b.Depart); +#14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select Cno,Sno,Degree from Score a where (select Degree from Score b where Cno='3-105' and b.Sno=a.Sno)>(select Degree from Score c where Cno='3-245' and c.Sno=a.Sno) order by Degree desc; +#15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select Cno,Sno,Degree from Score a where (select Degree from Score b where Cno='3-105' and b.Sno=a.Sno)>(select Degree from Score c where Cno='3-245' and c.Sno=a.Sno); + +#16 查询成绩比该课程平均成绩低的同学的成绩表。 +select Sno,Cno,Degree from Score a where a.Degree<(select AVG(Degree) from Score b where a.Cno=b.Cno); +#17 查询所有任课教师的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); +#18 查询所有未讲课的教师的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); +#19 查询“男”教师及其所上的课程。 +select Tname,Cname from Teacher,Course where Tsex='男' and Teacher.Tno=Course.Tno; +#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='李军') and Sname not in ('李军'); +#22 查询和“李军”同性别并同班的同学Sname. +select Sname from student where Ssex=(select Ssex from student where Sname='李军') and Sname not in ('李军') and Class=(select Class from student where Sname='李军'); +#23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 +select Sno,Degree from Score where Sno in (select Sno from student where Ssex='男') and Cno in (select Cno from Course where Cname='计算机导论'); +``` +