From 80b1b30e5092c13aa90111c227e6cedd6f0c4110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=99=E8=8B=8F=E6=96=87?= <2361635242@qq.com> Date: Mon, 20 Mar 2023 23:35:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230319\344\275\234\344\270\2324.md" | 184 ++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 "16 \351\230\231\350\213\217\346\226\207/20230319\344\275\234\344\270\2324.md" diff --git "a/16 \351\230\231\350\213\217\346\226\207/20230319\344\275\234\344\270\2324.md" "b/16 \351\230\231\350\213\217\346\226\207/20230319\344\275\234\344\270\2324.md" new file mode 100644 index 0000000..d1f4734 --- /dev/null +++ "b/16 \351\230\231\350\213\217\346\226\207/20230319\344\275\234\344\270\2324.md" @@ -0,0 +1,184 @@ +# 作业 + +```mysql +drop DATABASE if exists mtyk; +create DATABASE if not exists mtyk charset utf8; +use mtyk; +CREATE TABLE J_TEACHER ( + Tno INT PRIMARY KEY auto_increment COMMENT '教师编号', + Tname VARCHAR ( 10 ) UNIQUE COMMENT '教师姓名' +); + +create table J_STUDENT( +Sno int primary key auto_increment comment '学生编号', +Sname varchar(10) unique comment'学生姓名', +Sage date comment'学生年龄', +Ssex enum('男','女') comment'性别' +); + +create table J_COURSE( +Cno int primary key auto_increment comment '课程编号', +Cname varchar(10) unique comment'课程名称', +Tno int comment '授课教师编号', +foreign key (Tno) REFERENCES J_TEACHER (Tno) +); + +Create table J_SCORE( +Sno int comment'学生编号', +Cno int comment'课程编号', +Score int comment'成绩', +foreign key (Sno) references J_STUDENT (Sno), +foreign key (Cno) references J_COURSE (Cno) +); + +INSERT into J_teacher(tname) VALUES +('张老师'), +('王老师'), +('李老师'), +('赵老师'), +('刘老师'), +('向老师'), +('李文静'), +('叶平'); +SELECT * from j_teacher; + +INSERT INTO J_student +VALUES + ( 1, '张三', '1980-1-23', '男' ), + ( 2, '李四', '1982-12-12', '男' ), + ( 3, '张飒', '1981-9-9', '男' ), + ( 4, '莉莉', '1983-3-23', '女' ), + ( 5, '王弼', '1982-6-21', '男' ), + ( 6, '王丽', '1984-10-10', '女' ), + ( 7, '刘香', '1980-12-22', '女' ); + +INSERT INTO J_COURSE ( cname, tno ) +VALUES + ( '企业管理', 3 ), + ( '马克思', 1 ), + ( '美团盈开', 2 ), + ( '数据库', 5 ), + ( '物理', 8 ); + +INSERT INTO J_SCORE +VALUES + ( 1, 1, 80 ), + ( 1, 2, 86 ), + ( 1, 3, 83 ), + ( 1, 4, 89 ), + ( 2, 1, 50 ), + ( 2, 2, 36 ), + ( 2, 3, 43 ), + ( 2, 4, 59 ), + ( 3, 1, 50 ), + ( 3, 2, 96 ), + ( 3, 3, 73 ), + ( 3, 4, 69 ), + ( 4, 1, 90 ), + ( 4, 2, 36 ), + ( 4, 3, 88 ), + ( 4, 4, 99 ), + ( 5, 5, 90 ), + ( 5, 2, 96 ), + ( 5, 5, 98 ), + ( 5, 4, 99 ), + ( 6, 1, 70 ), + ( 6, 2, 66 ), + ( 6, 3, 58 ), + ( 6, 4, 79 ), + ( 7, 1, 80 ), + ( 7, 2, 76 ), + ( 7, 3, 68 ), + ( 7, 4, 59 ), + ( 7, 5, 89 ); +SELECT Sno from j_student where sname='刘香'; +SELECT Cno from j_course where Cname='美团盈开'; +SELECT * from j_score; + +-- 1、 修改刘香的美团盈开课程成绩为 100 分 +update j_score set Score=100 where sno=(SELECT Sno from j_student where sname='刘香') && Cno=(SELECT Cno from j_course where Cname='美团盈开'); +-- 2、 将学生表中的 sage 段放在 ssex 段后面 +alter table j_student MODIFY sage date after ssex; +-- 3、 教师表添加一个用于统计状态的列 名字为 now 默认值为有课 +alter table j_teacher add now varchar(10) default '有课'; +-- 4、 将没有上课的老师的 now 列值设置为无课 +update j_teacher set now='无课' where tno not in(SELECT tno from j_course); +-- 5、 修改课程表中的企业管理为比遛旺 +UPDATE j_course set cname='比遛旺' where cname='企业管理'; +-- 6、 查询课程 1 的成绩比课程 2 的成绩 高 的所有学生的学号 +SELECT a1.sno 学号 from (SELECT * from j_score where cno=1) a1 LEFT JOIN (SELECT * from j_score where cno=2) a2 on a1.sno=a2.sno where a1.score>a2.score; +-- 7、 查询平均成绩大于 60 分的同学的学号和平均成绩。 +SELECT a1.sno 学号,a1.平均成绩 from (SELECT sno ,round(avg(score),0) 平均成绩 from j_score GROUP BY sno) a1 LEFT JOIN (SELECT * from j_score ) a2 on a1.sno=a2.sno where score>平均成绩 ; +-- 7、(2) 查询每个同学的每门课程大于平均成绩的同学的学号和平均成绩。 +SELECT sno 学号,a2.平均成绩 from (SELECT * from j_score) a1 LEFT JOIN ( +SELECT cno,avg(score) 平均成绩 from j_score GROUP BY cno) a2 on a1.cno=a2.cno where a1.score>a2.`平均成绩`; +-- 8、 查询所有同学的学号、姓名、选课数、总成绩。 +SELECT s.sno,sname,count(cno),sum(score) from j_score s LEFT JOIN j_student stu on stu.sno=s.sno GROUP BY sno; +-- 9、 查询姓“李”的学生的个数。 +SELECT COUNT(Sname) from j_student where sname like '李%'; +-- 10、 查询没学过“叶平”老师课的同学的学号、姓名。 +SELECT sno,Sname from j_student where sno not in (SELECT + distinct sno +FROM + j_teacher tea + right JOIN j_course cour ON cour.tno = tea.Tno + LEFT JOIN j_score ON j_score.Cno = cour.Cno +WHERE + Tname = '叶平'); +-- 11、 查询同时学过课程 1 和课程 2 的同学的学号、姓名。 +SELECT sno,sname from j_student where sno in (SELECT stu.sno from j_score sco LEFT JOIN j_student stu on stu.sno =sco.sno where cno = 1 GROUP BY sco.sno) && sno in ( +SELECT stu.sno from j_score sco LEFT JOIN j_student stu on stu.sno =sco.sno where cno = 2 GROUP BY sco.sno); +-- 12、 查询学过“叶平”老师所教所有课程的所有同学的学号、姓名。 +SELECT sno,Sname from j_student where sno in (SELECT + distinct sno +FROM + j_teacher tea + right JOIN j_course cour ON cour.tno = tea.Tno + LEFT JOIN j_score ON j_score.Cno = cour.Cno +WHERE + Tname = '叶平'); +-- 13、 查询 课程编号 1 的成绩 比 课程编号 2 的成绩 高的所有同学的学号、姓名。 +SELECT sno,sname from j_student where sno in ( +SELECT c1.sno from (SELECT * from j_score where cno=1) c1 LEFT JOIN +(SELECT * from j_score where cno=2) c2 on c1.sno=c2.sno where c1.score>c2.score); +-- 14、 查询所有课程成绩小于 60 分的同学的学号、姓名。 +SELECT sno,sname from j_student where sno in (SELECT sno from j_score where score<60); +-- 15、 查询所有课程成绩大于 60 分的同学的学号、姓名。 +SELECT sno,sname from j_student where sno in (SELECT sno from j_score where score>60); +-- 16、 查询没有学全所有课的同学的学号、姓名 +SELECT sc.sno,sname from j_score sc LEFT JOIN j_student st on sc.sno=st.sno GROUP BY sc.sno having not count(sc.sno)=(select count(cno) from J_COURSE); +-- 17、 查询至少有一门课程 与 学号为 1 的同学所学课程 相同的同学的学号和姓名 +SELECT distinct sc.sno,sname from j_student st LEFT JOIN j_score sc on sc.sno=st.sno where sc.sno!=1 && cno in (SELECT cno from j_score where sno=1); +-- 18、 查询和 2 号同学学习的课程完全相同的其他同学学号和姓名。 +SELECT distinct sc.sno,sname from j_student st LEFT JOIN j_score sc on sc.sno=st.sno where sc.sno!=2 && cno (SELECT cno from j_score where sno=2); + +select sno from J_SCORE where sno!=2&& sno not in (select sno from J_SCORE where cno not in (select cno from J_SCORE where sno=2)) group by sno having count(sno)=(select count(cno) from J_SCORE where sno=2); + +-- 19、 查询各科成绩最高分和最低分。 +SELECT cno,max(score),min(score) from j_score GROUP BY cno; +-- 20、 查询每门课程被选修的学生数。 +SELECT cno,COUNT(cno) from j_score GROUP BY cno; +-- 21、 查询出只选修了一门课程的学生的学号和姓名。 +SELECT sc.sno,sname from j_score sc LEFT JOIN j_student st on st.sno=sc.sno GROUP BY sc.sno HAVING count(sc.cno)=1; +-- 22、 查询同名同性学生名单,并统计同名人数。 +select sname,ssex,count(sname) from (select a.sname,a.ssex from J_STUDENT a , J_STUDENT b where a.sname=b.sname and a.ssex=b.ssex and a.sno!=b.sno) c group by sname,ssex; +-- 23、 查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩。 + +SELECT sname,score from j_score s LEFT JOIN j_student sc on sc.sno=s.sno where cno=(SELECT cno from j_course where tno=(SELECT tno from j_teacher where Tname='叶平')) ORDER BY score desc limit 1; +-- 24、 查询不同课程成绩相同的学生的学号、课程号、学生成绩。 +SELECT a.sno,a.cno,a.score from j_score a JOIN j_score b where a.cno<>b.cno && a.score=b.Score; +-- 25、 查询每门课程成绩最好的前两名的学生 ID + +-- 26、 检索至少选修了 5 门课程的学生学号。 +SELECT sno from j_score GROUP BY sno HAVING count(cno)>=5; +-- 27、 查询没学过“叶平”老师讲授的课程的学生姓名。 +SELECT sname from j_student where sno in (SELECT distinct sno from j_score where cno!=(SELECT cno from j_course where tno=(SELECT tno from j_teacher where tname='叶平'))); +-- 28、 查询两门以上不及格课程的同学的学号及其平均成绩。 +SELECT sno,ROUND(avg(score)) from j_score where sno=( +SELECT DISTINCT a.sno from j_score a, j_score b where a.sno = b.sno && a.cno!=b.cno && a.score<60 && b.score<60) GROUP BY sno; +-- 29、 查询最受欢迎的老师(选修学生最多的老师)。 +SELECT COUNT(s.cno) a1,tname from j_score s LEFT JOIN j_course c on s.cno=c.cno LEFT JOIN j_teacher t on c.tno=t.tno GROUP BY s.cno ORDER BY a1 desc limit 1; +-- 30、 修改课程表名为给乱给要酒要 +alter TABLE j_course rename 给乱给要酒要; +``` + -- Gitee