2021/4/1 查询"01"课程比"02"课程成绩高的学生的信息及课程分数
-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
-- 确定学生的信息在Student表中,课程的分数在Course表中
select a.* ,b.s_score as 01_score,c.s_score as 02_score
from student a
join score b on a.s_id=b.s_id and b.c_id='01'
join score c on a.s_id=c.s_id and c.c_id='02'
where b.s_score>c.s_score
2021/4/1 查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT a.* , b.s_score 01_score , c.s_score 02_score FROM Student a
JOIN Score b ON a.s_id = b.s_id and b.c_id = '01'
JOIN Score c ON a.s_id = c.s_id and c.c_id ='02'
where b.s_score < c.s_score;
2021/4/1 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT a.s_id , a.s_name , AVG(b.s_score) avg_score FROM Student a
join Score b ON a.s_id = b.s_id
GROUP BY a.s_id HAVING avg_score >= 60;
2021/4/1查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 (包括有成绩的和无成绩的)
SELECT a.s_id , a.s_name , AVG(b.s_score) avg_score FROM Student a
JOIN Score b ON a.s_id = b.s_id GROUP BY a.s_id HAVING avg_score < 60
UNION
SELECT a.s_id , a.s_name , 0 as avg_score FROM Student a
where a.s_id not in(
SELECT DISTINCT s_id FROM score
)
2021/4/2查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT a.s_id , a.s_name , COUNT(DISTINCT(b.c_id)) , SUM(b.s_score) FROM Student a JOIN
Score b ON a.s_id = b.s_id GROUP BY a.s_id;
2021/4/2 查询"李"姓老师的数量
SELECT COUNT(a.t_id) FROM Teacher a where a.t_name like '李%'
2021/4/2 查询学过"张三"老师授课的同学的信息
SELECT a.*,d.t_name FROM Student a JOIN Score b ON a.s_id = b.s_id
JOIN Course c ON b.c_id = c.c_id
JOIN Teacher d ON c.t_id = d.t_id and d.t_name = '张三';
2021/4/2 查询没学过"张三"老师授课的同学的信息
SELECT * FROM Student WHERE s_id not in(
SELECT a.s_id FROM Student a JOIN Score b ON a.s_id = b.s_id
JOIN Course c ON b.c_id = c.c_id
JOIN Teacher d ON c.t_id = d.t_id and d.t_name = '张三'
);
2021/4/2 查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT * FROM Student where s_id in
(
SELECT s_id FROM Score where
s_id in (SELECT DISTINCT s_id FROM Score where c_id = '01')
and c_id = '02'
);
2021/4/2查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT * FROM Student where s_id in
(
SELECT s_id FROM Score where
s_id not in (SELECT DISTINCT s_id FROM Score where c_id = '02')
and c_id = '01'
);
2021/4/3查询没有学全所有课程的同学的信息
SELECT a.* FROM Student a where a.s_id not in(
SELECT a.s_id FROM Score a GROUP BY a.s_id HAVING COUNT(a.s_id) = (SELECT count(*) FROM Course)
)
SELECT a.* FROM student a LEFT JOIN score b ON b.s_id = a.s_id
GROUP BY a.s_id HAVING count(b.s_id) < (SELECT count(*) FROM course)
2021/4/3查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT DISTINCT a.* FROM student a JOIN score b ON a.s_id = b.s_id
where b.c_id in (SELECT c_id FROM score where s_id = '01')
2021/4/3(这个题太难了)查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT * FROM student where s_id in(
SELECT s_id FROM score where s_id in(
SELECT DISTINCT s_id FROM score where s_id in(
SELECT DISTINCT s_id FROM score where c_id = (SELECT c_id FROM score where s_id='01' LIMIT 0,1) and s_id != '01'
) and c_id = (SELECT c_id FROM score where s_id='01' LIMIT 1,1)
)and c_id = (SELECT c_id FROM score where s_id='01' LIMIT 2,1)
)
2021/4/4查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT s_name FROM student where s_id not in(
-- 学过张三老师的课学生的id
SELECT DISTINCT s_id FROM score where c_id = (
SELECT a.c_id FROM course a JOIN teacher b ON a.t_id = b.t_id where b.t_name = '张三'
)
)
2021/4/4查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT a.s_id , a.s_name , AVG(b.s_score) FROM student a JOIN score b
ON a.s_id = b.s_id where b.s_id in (
-- 查询两门及其以上不及格课程的同学的学号
SELECT s_id FROM score where s_score < 60 GROUP BY s_id HAVING count(s_id) >= 2
) GROUP BY b.s_id
2021/4/7检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT a.s_id , a.s_name , b.s_score FROM student a JOIN score b ON a.s_id=b.s_id
and b.c_id = '01' where b.s_score < 60 ORDER BY b.s_score desc
2021/4/7按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select a.s_id,(select s_score from score where s_id=a.s_id and c_id='01') as 语文,
(select s_score from score where s_id=a.s_id and c_id='02') as 数学,
(select s_score from score where s_id=a.s_id and c_id='03') as 英语,
round(avg(s_score),2) as 平均分 from score a GROUP BY a.s_id ORDER BY 平均分 DESC;
2021/4/7查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT a.c_id , b.c_name , max(a.s_score) as 最高分, min(a.s_score) as 最低分 , avg(a.s_score) as 平均分 , sum(case when a.s_score >= 60 then 1 else 0 end)/sum(case when a.s_score then 1 end) as 及格率 ,
sum(case when a.s_score >= 70 and a.s_score <= 80 then 1 else 0 end)/sum(case when a.s_score then 1 end) as 中等率,
sum(case when a.s_score >= 80 and a.s_score <= 90 then 1 else 0 end)/sum(case when a.s_score then 1 end) as 优良率,
sum(case when a.s_score >= 90 then 1 else 0 end)/sum(case when a.s_score then 1 end) as 优秀率
FROM score a JOIN course b ON a.c_id = b.c_id GROUP BY a.c_id
2021/4/8按各科成绩进行排序,并显示排名
1MySql模拟生成rownum的方法
Oracle中有一个伪列rownum,可以在生成查询结果表的时候生成一组递增的序列号。MySQL中没有这个伪列,但是有时候要用,可以用如下方法模拟生成一列自增序号。
(1)sql示例:
SELECT @i:=@i+1 as rownum ,s_id,s_name FROM student , (SELECT @i:=0) as init;
(2) 如果是多表联查,跟上述sql类似,连查完后定义一个初始化序列号即可:
SELECT @i:=@i+1 as rownum ,a.s_id , a.s_name , b.c_id , b.s_score FROM student a JOIN score b ON
a.s_id = b.s_id , (SELECT @i:=0) as init;
select s_id,c_id,s_score, @i:=@i+1 as rank from score,(SELECT @i:=0) as init GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
select a.s_id,a.c_id,
@i:=@i +1 as i保留排名,
@k:=(case when @score=a.s_score then @k else @i end) as rank不保留排名,
@score:=a.s_score as score
FROM score a, (select @k:=0,@i:=0,@score:=0) as init GROUP BY a.s_id ,a.c_id,a.s_score ORDER BY a.s_score desc
2021/4/8查询不同老师所教不同课程平均分从高到低显示
SELECT a.t_id as 老师ID , a.c_id as 课程号 , AVG(b.s_score) as 平均成绩 FROM course a JOIN
score b ON a.c_id = b.c_id GROUP BY a.t_id , b.c_id ORDER BY 平均成绩 desc
***2021/4/9查询各科成绩前三名的记录
SELECT a.s_id , a.c_id , a.s_score FROM score a left JOIN score b ON
a.c_id = b.c_id and a.s_score < b.s_score GROUP BY a.s_id,a.c_id,a.s_score
HAVING COUNT(b.s_id) < 3
ORDER BY a.c_id , a.s_score DESC