0
点赞
收藏
分享

微信扫一扫

Mysql 第一次作业

蓝莲听雨 2022-05-05 阅读 192

建表

create database wuxinyu;
use wuxinyu;

f0e23ce82f484bb4aa76131486de18d9.png

学生表student(SId,Sname,Sage,Ssex)

create table student(SId varchar(10),Sname varchar(10),Sbirth datetime,Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('07' , '王菊' , '1990-01-20' , '女');

60c89aa9c61f47119935215df0df0df5.png

课程表course(CId,Cname,TId) --CId --课程编号,Cname 课程名称,TId 教师编号

create table course(CId varchar(10),Cname nvarchar(10),TId varchar(10))
insert into course values('01' , '语文' , '02');
insert into course values('02' , '数学' , '01');
insert into course values('03' , '英语' , '04');
//改名 
rename  

 

3a21cc075552494593d03d0b5e199aea.png

 

教师表teacher(TId,Tname) --TId 教师编号,Tname 教师姓名

create table teacher(TId varchar(10),Tname varchar(10));
insert into teacher values('01' , '张三');
insert into teacher values('02' , '李四');
insert into teacher values('03' , '王五');

2603a53703234c1aa7c20811f7576ac0.png

成绩表 score(SId,CId,score) --SId 学生编号,CId 课程编号,score 分数

create table score(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into  score values('01' , '01' , 80);
insert into  score values('01' , '02' , 90);
insert into  score values('01' , '03' , 99);
insert into  score values('02' , '01' , 70);
insert into score values('02' , '02' , 60);
insert into score values('02' , '03' , 80);
insert into  score values('03' , '01' , 80);
insert into  score values('03' , '02' , 80);
insert into  score values('03' , '03' , 80);
insert into  scorevalues('04' , '01' , 50);
insert into  score values('04' , '02' , 30);
insert into  score values('04' , '03' , 20);
insert into  score values('05' , '01' , 76);
insert into  score values('05' , '02' , 87);
insert into  score values('06' , '01' , 31);
insert into  score values('06' , '03' , 34);
insert into  score values('07' , '02' , 89);
insert into  score values('07' , '03' , 98);

ef484376a3e84aaa83510d740295d138.png

1查询"01"课程比"02"课程成绩高的学生的信息及课程分数

select * from
(select score.SId, score.score from score where score.CId = '01') as t1 inner join
(select score.SId, score.score from score where score.CId = '02') as t2 on t1.SId = t2.SId
where t1.score > t2.score;

 9951caef8b204c47bfd0fec8c59f7d7d.png

 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

mysql> select sc1.sid,sc1.score score1,sc2.score score2
     from score sc1 
     join score sc2 on sc1.sid=sc2.sid
     where sc1.cid = 1 and sc2.cid = 2 and sc1.score<sc2.score;

8a752424aa4a4ba6b3490ebae80f0790.png

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

select s.SId,avgscore,Sname from(
select SId, AVG(score) as avgscore from score 
group by  SId 
having AVG(score)> 60
)r left join 
(select student.SId,student.Sname from
student)s on s.SId = r.SId;

fa16db43c47c4fb3a422e2e9e4748ada.png

 

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)

1

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

> select stu.sid,stu.sname,
    -> count(score.cid) countcourse,
    -> case when sum(score.score) is null then 0 else sum(score.score) end sumscore
    -> from student stu
    -> left join score
    -> on score.sid=stu.sid
    -> group by stu.sid,stu.sname;

d18ea25ddf7e4f38b4f83e8c9d18c32d.png

 

6、查询"李"姓老师的数量

 select count(*) from teacher where tname like '李%';

  1112e9bca6d043419db158b2f36f0ba5.png 

7、询学过"张三"老师授课的同学的信息


select student.* from student,teacher,course,score
where 
    student.sid = score.sid 
    and course.cid=score.cid 
    and course.tid = teacher.tid 
    and tname = '张三';

 e99fc637f99840c182f2b508eaa93844.png

 

8、查询没学过"张三"老师授课的同学的信息

select * from student
    where student.sid not in(
        select score.sid from score where score.cid in(
            select course.cid from course where course.tid in(
                select teacher.tid from teacher where tname = "张三"
            )
        )
    );

 

98ffb2f796ee47d98740abdab297270c.png

 

 

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select st.sid, st.sname from(
	select t1.sid from
		(select s1.sid from score s1 where s1.cid = 1) t1,
		(select s2.sid from score s2 where s2.cid = 2) t2
		where t1.sid = t2.sid
)stu, student st
where stu.sid = st.sid 

 60818782cd304c02b8c472808ef8a16a.png

 

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

1

 

11、查询没有学全所有课程的同学的信息

select * from student
where student.sid not in (
  select score.sid from score
  group by score.sid
  having count(score.cid)= (select count(cid) from course)
);

 af67ee785b8b44bc945c5d1b3c087f57.png

 

12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

select * from student 
where student.sid in (
    select score.sid from score 
    where score.cid in(
        select score.cid from score 
        where score.sid = '01'
    )
);

 7eb98fc1813f422c93330dfda0bc8037.png

 

13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

1

 

14、查询没学过"张三"老师讲授的任一门课程的学生姓名

select * from student
    where student.sid not in(
        select score.sid from score where score.cid in(
            select course.cid from course where course.tid in(
                select teacher.tid from teacher where tname = "张三"
            )
        )
    );

 90b1e4b51f1440dcbc3a7e1422c93154.png

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select student.SId, student.Sname,b.avg
from student right join
(select sid, AVG(score) as avg from score
    where sid in (
              select sid from score 
              where score<60 
              group by sid 
              having count(score)>1)
group by  sid) b on student.sid=b.sid;

 37002fa4c6464660bed793f26fc76203.png

 

16、检索"01"课程分数小于60,按分数降序排列的学生信息


group by  sid) b on student.sid=b.sid;
select student.*, score.score from student, score
where student.sid = score.sid
and score.score < 60
and cid = "01"
order by score.score desc;

 537bb988f45a498e8db6d2865772ef30.png

 

17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select *  from score 
left join (
    select sid,avg(score) as avscore from score 
    group by sid
    )r 
on score.sid = r.sid
order by avscore desc;

 f3c90bc29c4647d3b4ae0d2d5e06e675.png

18、查询各科成绩最高分、最低分和平均分,

select 
score.CId ,
max(score.score)as 最高分,
min(score.score)as 最低分,
AVG(score.score)as 平均分,
count(*)as 选修人数,
sum(case when score.score>=60 then 1 else 0 end )/count(*)as 及格率,
sum(case when score.score>=70 and score.score<80 then 1 else 0 end )/count(*)as 中等率,
sum(case when score.score>=80 and score.score<90 then 1 else 0 end )/count(*)as 优良率,
sum(case when score.score>=90 then 1 else 0 end )/count(*)as 优秀率 
from score
GROUP BY score.CId
ORDER BY count(*)DESC, score.CId ASC

2186b27a5680434f9c7c8a7f33405c71.png

 

19、按各科成绩进行排序,并显示排名

1

 

20、查询学生的总成绩并进行排名

select  sid,SUM(score)'总成绩' from score
group by sid
order by SUM(score) DESC

f26c6b3d527c4c0fa99812d3796aac1b.png

 

 

举报

相关推荐

MYSQL第一次作业

MySQL第一次作业

【MySQL】第一次作业

MYSQL的第一次作业

第一次python作业

python 第一次作业

Redis 第一次作业

Django第一次作业

0 条评论