diff --git "a/05 \346\236\227\344\274\237\345\275\254/20230311 \347\273\203\344\271\2403.md" "b/05 \346\236\227\344\274\237\345\275\254/20230311 \347\273\203\344\271\2403.md" new file mode 100644 index 0000000000000000000000000000000000000000..350e5cc3ae78b47b0bac52492d5621389752051d --- /dev/null +++ "b/05 \346\236\227\344\274\237\345\275\254/20230311 \347\273\203\344\271\2403.md" @@ -0,0 +1,170 @@ +# 作业 + +```mysql +CREATE database ten charset utf8; +use ten; +CREATE table student( -- 学生信息表 +stuid int not null primary key auto_increment , +stuname VARCHAR(20) not null, +stuage VARCHAR(11) not null, +stusex enum('1','0') not null, +time datetime +); + +DROP table student; +INSERT into student values +(null,'Tom',19,'1',null), +(null,'Jack',20,'0',null), +(null,'Rose',21,'1',null), +(null,'Lulu',19,'1',null), +(null,'Lili',21,'0',null), +(null,'abc',20,'1','2007-01-07 01:11:36.590'); + +CREATE table course( -- 课程信息表 +courseid int not null primary key auto_increment, +coursename VARCHAR(20) not null, +coursemarks char(4) not null +); +insert into course values +(null,'javabase',4), +(null,'html',2), +(null,'javascript',2), +(null,'sqibase',2); + +CREATE table score( -- 分数信息表 +scoreid int(3) not null primary key auto_increment, +stuid int(3) not null , +courseid int(3) not null , +score int(3) not null +); +insert into score values +(null,1,1,80),(null,1,2,85),(null,1,4,50), +(null,2,1,75),(null,2,3,45),(null,2,4,75), +(null,3,1,45),(null,4,1,95),(null,4,2,75), +(null,4,3,90),(null,4,4,45); +-- 题目: +-- 1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuname 姓名 , count(sc.courseid) 课程数量, avg(sc.score) +from student st ,course co , score sc +where st.stuid=sc.stuid AND co.courseid =sc.courseid +group by sc.stuid; + +-- 2.查询出每门课程的选修的学生的个数和学生考试的总分 +select count(sc.stuid) 课程选修的个数,sum(score) 考试总分 +from student st ,course co , score sc +where st.stuid=sc.stuid AND co.courseid =sc.courseid +group by sc.courseid; + +-- 3.查询出性别一样并且年龄一样的学生的信息 +select st1.*from student st1,student st2 +where st1.stusex=st2.stusex and st1.stuage = st2.stuage +and st1.stuid != st2.stuid; + +-- 4.查询出学分一样的课程信息 +select co1.* from course co1, course co2 +where co1.coursemarks = co2.coursemarks +and co1.courseid != co2.courseid +group by co1.courseid; + +-- 5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select sc.stuid 学号 , stuname 姓名, courseid 课程号 ,score 分数 +from student st ,score so, +(select stuid from score group by stuid) sc +where st.stuid=sc.stuid +and st.stuid=so.stuid ; + + +-- 6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select sc.stuid 学号, so.courseid 课程号, coursename 课程名, +coursemarks 课程学分, score +from student st ,score so ,course co, +(select stuid from score GROUP BY stuid) sc +where st.stuid=sc.stuid and st.stuid= so.stuid +and so.courseid=co.courseid; + +-- 7.查询出没有参加考试的学生的学号和姓名 +select stuid 学号, stuname 姓名 +from student st +WHERE stuid not IN(select stuid from score group by stuid); + +-- 8.查询出是周六周天来报到的学生 +select * , dayname(time) +from student +where time is not null +and dayname(time) in ('sunday','saturday'); + +-- 9.查询出姓名中有字母a的学生的信息 +select stuname from student +where stuname like "%a%"; + +-- 10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分,以及选修课程的数量 +#查询2门课程以上并且avg()>70 +select stuid 学号, avg(score) 平均分,count(stuid) 课程数量 +from score +group by stuid +having count(stuid) >2 +and avg(score) >70 ; +-- 题目: +-- 1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuname 姓名 , count(sc.courseid) 课程数量, avg(sc.score) +from student st ,course co , score sc +where st.stuid=sc.stuid AND co.courseid =sc.courseid +group by sc.stuid; + +-- 2.查询出每门课程的选修的学生的个数和学生考试的总分 +select count(sc.stuid) 课程选修的个数,sum(score) 考试总分 +from student st ,course co , score sc +where st.stuid=sc.stuid AND co.courseid =sc.courseid +group by sc.courseid; + +-- 3.查询出性别一样并且年龄一样的学生的信息 +select st1.*from student st1,student st2 +where st1.stusex=st2.stusex and st1.stuage = st2.stuage +and st1.stuid != st2.stuid; + +-- 4.查询出学分一样的课程信息 +select co1.* from course co1, course co2 +where co1.coursemarks = co2.coursemarks +and co1.courseid != co2.courseid +group by co1.courseid; + +-- 5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select sc.stuid 学号 , stuname 姓名, courseid 课程号 ,score 分数 +from student st ,score so, +(select stuid from score group by stuid) sc +where st.stuid=sc.stuid +and st.stuid=so.stuid ; + + +-- 6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select sc.stuid 学号, so.courseid 课程号, coursename 课程名, +coursemarks 课程学分, score +from student st ,score so ,course co, +(select stuid from score GROUP BY stuid) sc +where st.stuid=sc.stuid and st.stuid= so.stuid +and so.courseid=co.courseid; + +-- 7.查询出没有参加考试的学生的学号和姓名 +select stuid 学号, stuname 姓名 +from student st +WHERE stuid not IN(select stuid from score group by stuid); + +-- 8.查询出是周六周天来报到的学生 +select * , dayname(time) +from student +where time is not null +and dayname(time) in ('sunday','saturday'); + +-- 9.查询出姓名中有字母a的学生的信息 +select stuname from student +where stuname like "%a%"; + +-- 10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分,以及选修课程的数量 +#查询2门课程以上并且avg()>70 +select stuid 学号, avg(score) 平均分,count(stuid) 课程数量 +from score +group by stuid +having count(stuid) >2 +and avg(score) >70 ; +``` +