diff --git "a/38 \351\273\204\346\255\243\347\204\225/20230301 \347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\345\217\212\347\254\224\350\256\260.md" "b/38 \351\273\204\346\255\243\347\204\225/20230301 \347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\345\217\212\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..c095144e9f65b578e4fd8f20e8bd3fb432458871 --- /dev/null +++ "b/38 \351\273\204\346\255\243\347\204\225/20230301 \347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\345\217\212\347\254\224\350\256\260.md" @@ -0,0 +1,220 @@ +## 分组函数 + +所有函数都带括号 + +avg:平均值 + +max:最大 + +min:最小 + +count:统计 + +sum:求和 + +select 函数(字段名)from 表名; + +select 函数(函数(字段名))from 表名 + +## 联表查询 + +两个或多个表一起查询 + +条件:两个表必须有关联 + +image-20230305114950344 + + + +image-20230305115011427 + + + +image-20230305115043268 + + + +image-20230305115100663 + + + +image-20230305115119840 + + + +image-20230305115217417 + + + + + +image-20230305115232663 + + + +分组要求,group by分组字段 + +having 在统计中在次筛选 + +拼接字符串 + +例如:concat(x,y,z) + +concat_ws("连接符",x,y) + +自连接:一张表当两张表用,都要写别名,为了区分 + +笛卡尔积:无关联条件每一个都会匹配一遍会相乘 + +## 作业 + +```mysql +create database school charset utf8; +use school; +-- 创建学生表结构 +DROP TABLE student; +create table student( +sno varchar(20) primary key, +sname varchar(20) not null, +ssex varchar(20) not null, +sbirthday datetime, +class varchar(20) +); +-- 添加学生数据 +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); +select * from student; +-- 创建课程表结构 +show TABLES +drop table course; +create table course( +cno varchar(20), +cname varchar(20) not null, +tno varchar(20) not null, +primary KEY (cno,tno) + ); +alter table course add foreign key (tno) references teacher(tno); +-- 添加课程数据 +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'); + select * from course; +-- 创建成绩表结构 +DROP TABLE score; +create table score( +sno varchar(20) not null, +cno varchar(20) not null, +degree decimal(4,1), +PRIMARY KEY(sno,cno), +FOREIGN KEY (sno) REFERENCES student(sno), +FOREIGN KEY (cno) REFERENCES course(cno) +); +-- 添加成绩 +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); +select * from score; +-- 创建老师表结构 +DROP TABLE teacher +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) +); +SELECT * FROM teacher; +DESC teacher; +show TABLES; +-- 添加教师数据 +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 + stu.*,c.* + from + student stu + left join score s on stu.sno = s.sno + left join course c on s.cno = c.cno; +-- 2.查询没有学生的教师所有信息 + select + t.* + from + teacher t + left join course c on c.tno = t.tno + left join score s on s.cno=c.cno + where sno is null; +-- 3.查询score表中得最高分的学生学号和课程号 +select max(degree),sno,cno from score; +select sno,cno,degree from score where degree = ( select MAX( degree ) from score ); +-- 4.查询所有学生的sname cno degree列 +select sname,cno,degree from student,score; +-- 5.查询所有学生的sno cname degree列 +select sno,cname,degree from course,score; +-- 6.查询所有学生的sname cname degree列 +select sname,cname,degree from course,score,student; +-- 7.查询 95033 班学生得平均分 +select AVG(degree) from student,score where Class=95033; +-- 8.查询选修 3-105 课程得成绩高于 109 号同学成绩的所有同学的记录 +select a.* from score a inner join score b on a.cno=b.cno where a.cno='3-105' and b.sno=109 and a.degree > b.degree order by a.sno; +-- 9.查询score中选学多门课程得同学中 分数非最高分成绩的记录 +select MAX(degree) from score; +select sno,cno,degree from score where degree != (select MAX(degree) from score); +-- 10.查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from score where cno='3-105' and degree>(select degree from score where cno='3-105' and sno='109'); +-- 11.查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select year(sbirthday) from student where sno='108'; +select sno ,sname,sbirthday from student where year(sbirthday)=(select year(sbirthday) from student where sno='108'); +-- 12.查询“张旭“教师任课的学生成绩。 +select * from teacher where tname='张旭'; +select degree from teacher t ,course c ,score s where t.tno=c.tno and c.cno=s.cno and t.tname='张旭'; +-- 13.查询选修某课程的同学人数多于5人的教师姓名。 +select tname from course co,teacher te where co.tno=te.tno and cno=(select cno from score group by cno having count(*)>5); +-- 14.查询出“计算机系“教师所教课程的成绩表。 +select sc.* from course co, score sc where co.cno = sc.cno and co.tno in(select tno from teacher where depart='计算机系'); +-- 15.查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select tname ,prof from teacher where depart='计算机系' +union +select tname ,prof from teacher where depart='电子工程系'; +-- 16.查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select cno,sno,degree from score where cno='3-105' and degree>all(select degree from score where cno='3-245') order by degree desc; +-- 17.查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select * from score where cno='3-105' and degree>all(select degree from score where cno='3-245'); +-- 18.查询成绩比该课程平均成绩低的同学的成绩表。 +select * from score a where a.degree<(select avg(degree) from score b where a.cno=b.cno); +-- 19.查询所有任课教师的Tname和Depart. +select tname,depart from teacher; +-- 20.查询所有未讲课的教师的Tname和Depart. +select te.tname,te.depart from teacher te left join course co on te.tno = co.tno left join score sc on co.cno = sc.cno where sc.sno is null ; +-- 21.查询“男”教师及其所上的课程。 +select * from teacher te,course co where te.tno=co.tno and tsex='男'; +-- 22.查询最高分同学的Sno、Cno和Degree列。 +select sno,cno,degree from score where cno in(select cno from score group by cno) and degree in (select max(degree) from score group by cno); +-- 23.查询和“李军”同性别的所有同学的Sname. +Select sname from student where ssex=(select ssex from student where sname='李军'); +-- 24.查询和“李军”同性别并同班的同学Sname. +select sname from student where ssex=(select ssex from student where sname='李军') and class=(select class from student where sname='李军'); +-- 25.查询所有选修“计算机导论”课程的“男”同学的成绩表。 +select * from student st , score sc ,course co where st.sno=sc.sno and co.cno=sc.cno and st.ssex='男' and co.cname='计算机导论'; + +``` + diff --git "a/38 \351\273\204\346\255\243\347\204\225/20230306 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\345\217\212\347\254\224\350\256\260.md" "b/38 \351\273\204\346\255\243\347\204\225/20230306 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\345\217\212\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..d072296584405da2ba4dfd8cd921cf72ccaaa076 --- /dev/null +++ "b/38 \351\273\204\346\255\243\347\204\225/20230306 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\345\217\212\347\254\224\350\256\260.md" @@ -0,0 +1,134 @@ +## 子查询 + +嵌套在其他sql语句中的select语句 + +子查询的结果 + +可能作为外层查询的条件,数据源及要查的结果 + +形式一: + +放在select后,单列单值 + +形式二: + +放在where/having后 单列单值、单列多值 + +形式三: + +放在from后面 多列多值 + +select (形式一)from (形式三) where (形式二) + +## 作业 + +```mysql +drop database zcx; +create database zcx charset utf8; +use zuoye; +SHOW TABLES; +-- 学生信息表 +create table stuinfo( +stuNo varchar(10) primary key, +stuName varchar(10) not null, +stuSex enum('男','女'), +stuAge int, +stuAddress varchar(20), +stuSeat int +); +DROP TABLE stuinfo; +insert into stuinfo values('s2501','张秋利','男',20,'美国硅谷',1); +insert into stuinfo values('s2502','李斯文','女',18,'湖北武汉',2); +insert into stuinfo values('s2503','马文才','男',18,'湖南长沙',3); +insert into stuinfo values('s2504','欧阳俊雄','女',21,'湖北武汉',4); +insert into stuinfo values('s2505','梅超风','男',20,'湖北武汉',5); +insert into stuinfo values('s2506','陈旋风','男',19,'美国硅谷',6); +select * from stuinfo; +desc stuinfo; +-- 成绩表 +create table stuExam( +examNo int not null primary key, +stuNo varchar(10) not null, +writterExam int, +labExam int +); +insert into stuExam values(1,'s2501',50,70); +insert into stuExam values(2,'s2502',60,65); +insert into stuExam values(3,'s2503',86,70); +insert into stuExam values(4,'s2504',40,80); +insert into stuExam values(5,'s2505',70,85); +insert into stuExam values(6,'s2506',85,90); +select * from stuExam; +desc stuExam; +-- 成绩表 +drop table stuMarks; +create table stuMarks( +examNo int not null primary key, +stuID varchar(10) not null, +score int +); +insert into stuMarks values(1,'s2501',88); +insert into stuMarks values(2,'s2501',92); +insert into stuMarks values(3,'s2501',53); +insert into stuMarks values(4,'s2502',60); +insert into stuMarks values(5,'s2502',99); +insert into stuMarks values(6,'s2503',82); +select * from stuMarks; +desc stuMarks; +-- 在如图的数据表上完成以下题目 +-- 1.查询出年龄比班上平均年龄大的学生的信息 +select avg(stuAge) from stuinfo; +select * from stuinfo where stuAge > (select avg(stuAge) from stuinfo); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +SELECT + stui.stuNo, + stui.stuName, + stui.stuSex, + max( score ) +FROM + stuMarks stum + RIGHT JOIN stuinfo stui ON stum.stuID = stui.stuNo +GROUP BY + stuID; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) + select *,avg(writterExam + labExam) from stuExam GROUP BY stuNo; + +SELECT stui.stuNo, stui.stuName, stui.stuSex,( writterExam + labExam )/ 2 +FROM + stuExam stue + RIGHT JOIN stuinfo stui ON stue.stuNo = stui.stuNo +GROUP BY + stui.stuNo; +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuSex = '男' and stuAge >= 20; +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +SELECT stuage FROM stuinfo WHERE stusex = '男'; +SELECT * FROM stuinfo WHERE stusex = '女'and stuage>ALL(SELECT stuage FROM stuinfo WHERE stusex = '男') +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +SELECT * FROM stumarks; + SELECT * FROM stuinfo LEFT JOIN stumarks ON stuinfo.stuno = stumarks.stuid + WHERE score>=60 +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + select DISTINCT * from stuMarks stum right join stuinfo stui on stum.stuID=stui.stuNo where stum.score is not null ; +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + select * from stuMarks stum right join stuinfo stui on stum.stuID=stui.stuNo where stum.score is null; +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) + select * from stuMarks stum right join stuinfo stui on stum.stuID=stui.stuNo where stum.score > 90; +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +SELECT * FROM stumarks; +SELECT AVG(score),stuinfo.* FROM stumarks RIGHT JOIN stuinfo ON stumarks.stuid = stuinfo.stuno GROUP BY examno,stuinfo.stuno HAVING AVG(score)>80; +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select max(score) from stumarks; +select score from stumarks where stuid='s2501'; +select * from stuinfo left join stumarks on stuno = stuid where score>all(select score from stumarks where stuid='s2501'); +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +SELECT score FROM stumarks WHERE stuid = 's2501'; +SELECT * FROM stumarks WHERE score > ANY(SELECT score FROM stumarks WHERE stuid = 's2501') +-- 13.查询班上比所有男生年龄都要大的女生的信息 + select max(stuage) from stuinfo where stusex='男'; + select * from stuinfo where stusex = '女' and stuage >(select max(stuage) from stuinfo where stusex='男'); +-- 14.查询出只是比某个男生年龄大的女生的信息 + SELECT stuage FROM stuinfo WHERE stusex = '男'; + SELECT * FROM stuinfo WHERE stusex = '女' AND stuage>any(SELECT stuage FROM stuinfo WHERE stusex = '男'); +``` +