diff --git "a/40 \351\237\251\351\234\207\346\264\213/20230227 \345\205\263\350\201\224\346\237\245\350\257\242\345\220\210\345\271\266\350\241\250.md" "b/40 \351\237\251\351\234\207\346\264\213/20230227 \345\205\263\350\201\224\346\237\245\350\257\242\345\220\210\345\271\266\350\241\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..e47dd19b951a82856bdf3778094007a8798a3b8a --- /dev/null +++ "b/40 \351\237\251\351\234\207\346\264\213/20230227 \345\205\263\350\201\224\346\237\245\350\257\242\345\220\210\345\271\266\350\241\250.md" @@ -0,0 +1,148 @@ +# 1笔记 + +聚合函数;多行,通过聚合会变成一行,或者少行 + +1.求和 sum() + +```mysql +select sum(salary) from temployee; +``` + + + +2.求平均值 avg() + +```mysql +select avg(salary) from employee; +``` + + + +3.求最大值 max() + +```mysql +select max(salary) from employee; +``` + + + +4.求最小值min() + +```mysql +select min(salary) from employee; +``` + + + +5。求数量 count() + +```mysql +select count(num) from employee; +``` + +保留两位小数 + +```mysql +select round(avg(salary),2) from empoyee; +``` + + + +6.分组;group by + +7 关联 ; inner join ...on left join ...on right join ...on union + +```mysql +select * from student inner join class on student.classid = classid; +``` + + + +# 2作业 + + + +```mysql +create database zhu charset utf8; +show databases; +use zhu; +create table student( + sno varchar(20) primary key, + sname varchar(20) not null, + ssex varchar(20) not null, + sbirthday datetime null, + class varchar(20) 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); +create table course( + cno varchar(20) not null primary key, + cname varchar(20) not null, + tno varchar(20) not null + forign 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); +create table score( + sno varchar(20), + cno varchar(20), + degree decimal(4,1) null, + primary key(sno,cno) + foreign key (sno) references student(sno) on update cascade on delete cascade +); + +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); +create table teacher( + tno varchar(20) primary key, + tname varchar(20) not null, + tsex varchar(20) not null, + tbirthday datetime null, + prof varchar(20) null, + depart varchar(20) not null +); +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','助教','电子工程系'); +#① 查询Score表中的最高分的学生学号和课程号。 +select sno,cno from score where degree = (select max(degree) from score); +#② 查询所有学生的Sname、Cno和Degree列。 +select sname,cno,degree from student inner join course on student.cno = course.cno; +#③ 查询所有学生的Sno、Cname和Degree列。 +select sno,cname,degree from student inner join score on student.sno = score.sno; +#④ 查询所有学生的Sname、Cname和Degree列。 +select sname,cname,degree from student inner join score on student.sno = score.sno and stuent.cno = course.cno; +#⑤ 查询“95033”班学生的平均分。 +select avg(degree) from student where class = 95033; +#⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select * from student inner join score on student.Sno = score.Sno and score.Cno = '3-105' and score.Degree > 76; +#⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select * from student inner join score on student.Sno = score.Sno and score.Degree = (select !(max(score.Degree)) from score); +#⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from student inner join score on student.Sno = score.Sno and score.Cno = '3-105' and score.Degree > 76; +#⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select Sno,Sname,Sbirthday from student where year(Sbirthday) = '1977'; +#⑩ 查询“张旭“教师任课的学生成绩。 +select score.Degree from score inner join course on score.Cno = course.Cno; + +#11 查询选修某课程的同学人数多于5人的教师姓名。 +select teacher.Tname from teacher inner join course on teacher.Tno = course.Tno + inner join score on course.Cno = score.Cno + and score.Cno = (select count(Cno) > 5 from score); +#12 查询出“计算机系“教师所教课程的成绩表。 +select score.Sno,score.Cno,score.Degree from course inner join score on course.Cno = score.Cno + inner join teacher on course.Tno = teacher.Tno and teacher.Depart = '计算机系'; +#13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select Tname,Prof from teacher where Prof in (select distinct Prof from teacher); +#14 查询选修编号为“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 > 75 order by score.Degree desc; +``` +