一、创建数据库和表 数据库 学生表 student 课程表 course 教师表 teacher
数据库
学生表 student
课程表 course
教师表 teacher
成绩表 score
表关系
drop database if exists Mysql_test cascade;create database mysql_test;use mysql_test;
create table student ( s_id int, s_name varchar(8), s_birth date, s_sex varchar(4));
insert into student values(1,'赵雷','1990-01-01','男'),(2,'钱电','1990-12-21','男'),(3,'孙风','1990-05-20','男'),(4,'李云','1990-08-06','男'),(5,'周梅','1991-12-01','女'),(6,'吴兰','1992-03-01','女'),(7,'郑竹','1989-07-01','女'),(8,'王菊','1990-01-20','女');
create table course ( c_id int, c_name varchar(8), t_id int);
insert into course values(1,'语文',2),(2,'数学',1),(3,'英语',3);
create table teacher ( t_id int, t_name varchar(8));
insert into teacher values(1,'张三'),(2,'李四'),(3,'王五');
create table score ( s_id int, c_id int, s_score int);
insert into score values(1,1,80),(1,2,90),(1,3,99),(2,1,70),(2,2,60),(2,3,65),(3,1,80),(3,2,80),(3,3,80),(4,1,50),(4,2,30),(4,3,40),(5,1,76),(5,2,87),(6,1,31),(6,3,34),(7,2,89),(7,3,98);
select s.*,sc1.s_score as score_01,sc2.s_score as score_02from student sinner join ( select * from score where c_id = 1) sc1on s.s_id = sc1.s_idinner join ( select * from score where c_id = 2) sc2on s.s_id = sc2.s_idwhere sc1.s_score > sc2.s_score;
+------+--------+------------+-------+----------+----------+
| s_id | s_name | s_birth | s_sex | score_01 | score_02 |
+------+--------+------------+-------+----------+----------+
| 2 | 钱电 | 1990-12-21 | 男 | 70 | 60 |
| 4 | 李云 | 1990-08-06 | 男 | 50 | 30 |
+------+--------+------------+-------+----------+----------+
select s.*,sc1.s_score as score_01,sc2.s_score as score_02from student sinner join ( select * from score where c_id = 1) sc1on s.s_id = sc1.s_idinner join ( select * from score where c_id = 2) sc2on s.s_id = sc2.s_idwhere sc1.s_score < sc2.s_score;
+------+--------+------------+-------+----------+----------+
| s_id | s_name | s_birth | s_sex | score_01 | score_02 |
+------+--------+------------+-------+----------+----------+
| 1 | 赵雷 | 1990-01-01 | 男 | 80 | 90 |
| 5 | 周梅 | 1991-12-01 | 女 | 76 | 87 |
+------+--------+------------+-------+----------+----------+
select s.s_id,s.s_name,round(avg_score, 2) as avg_scorefrom student sinner join ( select s_id,avg(s_score) as avg_score from score group by s_id having avg_score >= 60) t1on s.s_id = t1.s_id;
+------+--------+-----------+
| s_id | s_name | avg_score |
+------+--------+-----------+
| 1 | 赵雷 | 89.67 |
| 2 | 钱电 | 65.00 |
| 3 | 孙风 | 80.00 |
| 5 | 周梅 | 81.50 |
| 7 | 郑竹 | 93.50 |
+------+--------+-----------+
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
select s.s_id,s.s_name,ifnull(round(avg_score, 2), 0) as avg_scorefrom student sleft join ( select s_id,avg(s_score) as avg_score from score group by s_id) t1on s.s_id = t1.s_idwhere avg_score is null or avg_score < 60;
+------+--------+-----------+
| s_id | s_name | avg_score |
+------+--------+-----------+
| 4 | 李云 | 40.00 |
| 6 | 吴兰 | 32.50 |
| 8 | 王菊 | 0.00 |
+------+--------+-----------+
select s.s_id,s.s_name,ifnull(cnt_course, 0) as cnt_course,ifnull(sum_score, 0) as sum_scorefrom student sleft join ( select s_id,count(*) as cnt_course,sum(s_score) as sum_score from score group by s_id) t1on s.s_id = t1.s_id;
+------+--------+------------+-----------+
| s_id | s_name | cnt_course | sum_score |
+------+--------+------------+-----------+
| 1 | 赵雷 | 3 | 269 |
| 2 | 钱电 | 3 | 195 |
| 3 | 孙风 | 3 | 240 |
| 4 | 李云 | 3 | 120 |
| 5 | 周梅 | 2 | 163 |
| 6 | 吴兰 | 2 | 65 |
| 7 | 郑竹 | 2 | 187 |
| 8 | 王菊 | 0 | 0 |
+------+--------+------------+-----------+
select count(*) as cnt_name_lifrom teacherwhere t_name like '李%';
+-------------+
| cnt_name_li |
+-------------+
| 1 |
+-------------+
select s.*from student swhere s_id in ( select distinct s_id from score sc inner join ( select c_id from course c inner join teacher t on c.t_id = t.t_id where t_name = '张三' ) t1 on sc.c_id = t1.c_id);
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 1 | 赵雷 | 1990-01-01 | 男 |
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
| 5 | 周梅 | 1991-12-01 | 女 |
| 7 | 郑竹 | 1989-07-01 | 女 |
+------+--------+------------+-------+
select s.*from student swhere s_id not in ( select distinct s_id from score sc inner join ( select c_id from course c inner join teacher t on c.t_id = t.t_id where t_name = '张三' ) t1 on sc.c_id = t1.c_id);
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 6 | 吴兰 | 1992-03-01 | 女 |
| 8 | 王菊 | 1990-01-20 | 女 |
+------+--------+------------+-------+
select s.*from student swhere s_id in ( select s_id from score where c_id in (1,2) group by s_id having count(*) = 2);
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 1 | 赵雷 | 1990-01-01 | 男 |
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
| 5 | 周梅 | 1991-12-01 | 女 |
+------+--------+------------+-------+
select s.*from student swhere s_id in ( select s_id from score where c_id = 1 and s_id not in ( select s_id from score where c_id = 2 ));
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 6 | 吴兰 | 1992-03-01 | 女 |
+------+--------+------------+-------+
select *from studentwhere s_id not in ( select s_id from score group by s_id having count(*) = ( select count(*) from course ));
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 5 | 周梅 | 1991-12-01 | 女 |
| 6 | 吴兰 | 1992-03-01 | 女 |
| 7 | 郑竹 | 1989-07-01 | 女 |
| 8 | 王菊 | 1990-01-20 | 女 |
+------+--------+------------+-------+
select *from studentwhere s_id in ( select distinct s_id from score s inner join ( select c_id from score where s_id = 1 ) t1 on s.c_id = t1.c_id);
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 1 | 赵雷 | 1990-01-01 | 男 |
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
| 5 | 周梅 | 1991-12-01 | 女 |
| 6 | 吴兰 | 1992-03-01 | 女 |
| 7 | 郑竹 | 1989-07-01 | 女 |
+------+--------+------------+-------+
select *from studentwhere s_id in ( select s_id from score s inner join ( select c_id from score where s_id = 1 ) t1 on s.c_id = t1.c_id where s_id != 1 group by s_id having count(*) = ( select count(*) from score where s_id = 1 ));
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
+------+--------+------------+-------+
查询没学过"张三"老师讲授的任一门课程的学生姓名
select s_namefrom studentwhere s_id not in ( select s_id from score sc inner join ( select c_id from course c inner join teacher t on c.t_id = t.t_id where t_name = '张三' ) t1 on sc.c_id = t1.c_id);
+--------+
| s_name |
+--------+
| 吴兰 |
| 王菊 |
+--------+
select s.s_id,s.s_name,round(avg(s_score), 2) as avg_scorefrom student sinner join ( select s_id from score where s_score < 60 group by s_id having count(*) >= 2) t1on s.s_id = t1.s_idinner join score sc on s.s_id = sc.s_idgroup by s.s_id;
+------+--------+-----------+
| s_id | s_name | avg_score |
+------+--------+-----------+
| 4 | 李云 | 40.00 |
| 6 | 吴兰 | 32.50 |
+------+--------+-----------+
检索"01"课程分数小于60,按分数降序排列的学生信息
select s.*,s_score as score_01from student sinner join ( select s_id,s_score from score where c_id = 1 and s_score < 60) t1on s.s_id = t1.s_idorder by score_01 desc;
+------+--------+------------+-------+----------+
| s_id | s_name | s_birth | s_sex | score_01 |
+------+--------+------------+-------+----------+
| 4 | 李云 | 1990-08-06 | 男 | 50 |
| 6 | 吴兰 | 1992-03-01 | 女 | 31 |
+------+--------+------------+-------+----------+
select s.s_id as 学号,s.s_name as 姓名, sum(case c_id when 1 then s_score else 0 end) as 语文, sum(case c_id when 2 then s_score else 0 end) as 数学, sum(case c_id when 3 then s_score else 0 end) as 英语, ifnull(round(avg(s_score), 2), 0) as 平均成绩from student sleft join score sc on s.s_id = sc.s_idgroup by s.s_idorder by 平均成绩 desc;
+------+------+------+------+------+----------+
| 学号 | 姓名 | 语文 | 数学 | 英语 | 平均成绩 |
+------+------+------+------+------+----------+
| 7 | 郑竹 | 0 | 89 | 98 | 93.50 |
| 1 | 赵雷 | 80 | 90 | 99 | 89.67 |
| 5 | 周梅 | 76 | 87 | 0 | 81.50 |
| 3 | 孙风 | 80 | 80 | 80 | 80.00 |
| 2 | 钱电 | 70 | 60 | 65 | 65.00 |
| 4 | 李云 | 50 | 30 | 40 | 40.00 |
| 6 | 吴兰 | 31 | 0 | 34 | 32.50 |
| 8 | 王菊 | 0 | 0 | 0 | 0.00 |
+------+------+------+------+------+----------+
select c.c_id as 课程ID,c.c_name as 课程name, max(s_score) as 最高分, min(s_score) as 最低分, round(avg(s_score), 2) as 平均分, concat(round(sum(case when s_score >= 60 then 1 else 0 end) / count(*) * 100, 2), '%') as 及格率, concat(round(sum(case when s_score between 70 and 80 then 1 else 0 end) / count(*) * 100, 2), '%') as 中等率, concat(round(sum(case when s_score between 80 and 90 then 1 else 0 end) / count(*) * 100, 2), '%') as 优良率, concat(round(sum(case when s_score >= 90 then 1 else 0 end) / count(*) * 100, 2), '%') as 优秀率from course cinner join score s on c.c_id = s.c_idgroup by c.c_id;
+--------+----------+--------+--------+--------+--------+--------+--------+--------+
| 课程ID | 课程name | 最高分 | 最低分 | 平均分 | 及格率 | 中等率 | 优良率 | 优秀率 |
+--------+----------+--------+--------+--------+--------+--------+--------+--------+
| 1 | 语文 | 80 | 31 | 64.50 | 66.67% | 66.67% | 33.33% | 0.00% |
| 2 | 数学 | 90 | 30 | 72.67 | 83.33% | 16.67% | 66.67% | 16.67% |
| 3 | 英语 | 99 | 34 | 69.33 | 66.67% | 16.67% | 16.67% | 33.33% |
+--------+----------+--------+--------+--------+--------+--------+--------+--------+
select (@i := case when @pre_group_id = c_id then @i + 1 else 1 end) as rank, (@pre_group_id := c_id) as c_id, c_name,s_id,s_name,s_scorefrom (select @i := 0, @pre_group_id := 1) varcross join ( select c.c_id,c.c_name,s.s_id,s.s_name,s_score from score sc inner join student s on sc.s_id = s.s_id inner join course c on sc.c_id = c.c_id group by c.c_id,s.s_id order by c.c_id,s_score desc) t1;
+------+------+--------+------+--------+---------+
| rank | c_id | c_name | s_id | s_name | s_score |
+------+------+--------+------+--------+---------+
| 1 | 1 | 语文 | 1 | 赵雷 | 80 |
| 2 | 1 | 语文 | 3 | 孙风 | 80 |
| 3 | 1 | 语文 | 5 | 周梅 | 76 |
| 4 | 1 | 语文 | 2 | 钱电 | 70 |
| 5 | 1 | 语文 | 4 | 李云 | 50 |
| 6 | 1 | 语文 | 6 | 吴兰 | 31 |
| 1 | 2 | 数学 | 1 | 赵雷 | 90 |
| 2 | 2 | 数学 | 7 | 郑竹 | 89 |
| 3 | 2 | 数学 | 5 | 周梅 | 87 |
| 4 | 2 | 数学 | 3 | 孙风 | 80 |
| 5 | 2 | 数学 | 2 | 钱电 | 60 |
| 6 | 2 | 数学 | 4 | 李云 | 30 |
| 1 | 3 | 英语 | 1 | 赵雷 | 99 |
| 2 | 3 | 英语 | 7 | 郑竹 | 98 |
| 3 | 3 | 英语 | 3 | 孙风 | 80 |
| 4 | 3 | 英语 | 2 | 钱电 | 65 |
| 5 | 3 | 英语 | 4 | 李云 | 40 |
| 6 | 3 | 英语 | 6 | 吴兰 | 34 |
+------+------+--------+------+--------+---------+
select (@i := @i + 1) as rank,t2.*from (select @i := 0) varcross join ( select s.s_id,s.s_name,sum_score from student s inner join ( select s_id,sum(s_score) as sum_score from score group by s_id ) t1 on s.s_id = t1.s_id order by sum_score desc) t2;
+------+------+--------+-----------+
| rank | s_id | s_name | sum_score |
+------+------+--------+-----------+
| 1 | 1 | 赵雷 | 269 |
| 2 | 3 | 孙风 | 240 |
| 3 | 2 | 钱电 | 195 |
| 4 | 7 | 郑竹 | 187 |
| 5 | 5 | 周梅 | 163 |
| 6 | 4 | 李云 | 120 |
| 7 | 6 | 吴兰 | 65 |
+------+------+--------+-----------+
select t.t_id,t.t_name,c.c_id,c.c_name,round(avg(s_score), 2) as avg_scorefrom score scinner join course c on sc.c_id = c.c_idinner join teacher t on t.t_id = c.t_idgroup by t.t_id,c.c_idorder by t.t_id,avg_score desc;
+------+--------+------+--------+-----------+
| t_id | t_name | c_id | c_name | avg_score |
+------+--------+------+--------+-----------+
| 1 | 张三 | 2 | 数学 | 72.67 |
| 2 | 李四 | 1 | 语文 | 64.50 |
| 3 | 王五 | 3 | 英语 | 69.33 |
+------+--------+------+--------+-----------+
select *from ( select (@i := case when @pre_group_id = c_id then @i + 1 else 1 end) as rank, (@pre_group_id := c_id) as c_id, c_name,s_id,s_name,s_birth,s_sex,s_score from (select @i := 0, @pre_group_id := 1) var cross join ( select c.c_id,c.c_name,s.*,s_score from score sc inner join student s on sc.s_id = s.s_id inner join course c on sc.c_id = c.c_id group by c.c_id,s.s_id order by c.c_id,s_score desc ) t1) t2where rank in (2,3);
+------+------+--------+------+--------+------------+-------+---------+
| rank | c_id | c_name | s_id | s_name | s_birth | s_sex | s_score |
+------+------+--------+------+--------+------------+-------+---------+
| 2 | 1 | 语文 | 3 | 孙风 | 1990-05-20 | 男 | 80 |
| 3 | 1 | 语文 | 5 | 周梅 | 1991-12-01 | 女 | 76 |
| 2 | 2 | 数学 | 7 | 郑竹 | 1989-07-01 | 女 | 89 |
| 3 | 2 | 数学 | 5 | 周梅 | 1991-12-01 | 女 | 87 |
| 2 | 3 | 英语 | 7 | 郑竹 | 1989-07-01 | 女 | 98 |
| 3 | 3 | 英语 | 3 | 孙风 | 1990-05-20 | 男 | 80 |
+------+------+--------+------+--------+------------+-------+---------+
select c.c_id as 课程编号,c.c_name as 课程名称, sum(case when s_score between 85 and 100 then 1 else 0 end) as '[100-85]', concat(round(sum(case when s_score between 85 and 100 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比, sum(case when s_score between 70 and 85 then 1 else 0 end) as '[85-70]', concat(round(sum(case when s_score between 70 and 85 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比, sum(case when s_score between 60 and 70 then 1 else 0 end) as '[70-60]', concat(round(sum(case when s_score between 60 and 70 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比, sum(case when s_score between 0 and 60 then 1 else 0 end) as '[0-60]', concat(round(sum(case when s_score between 0 and 60 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比from course cinner join score scon c.c_id = sc.c_idgroup by c.c_id;
+----------+----------+----------+--------+---------+--------+---------+--------+--------+--------+
| 课程编号 | 课程名称 | [100-85] | 百分比 | [85-70] | 百分比 | [70-60] | 百分比 | [0-60] | 百分比 |
+----------+----------+----------+--------+---------+--------+---------+--------+--------+--------+
| 1 | 语文 | 0 | 0.00% | 4 | 66.67% | 1 | 16.67% | 2 | 33.33% |
| 2 | 数学 | 3 | 50.00% | 1 | 16.67% | 1 | 16.67% | 2 | 33.33% |
| 3 | 英语 | 2 | 33.33% | 1 | 16.67% | 1 | 16.67% | 2 | 33.33% |
+----------+----------+----------+--------+---------+--------+---------+--------+--------+--------+
select (@i := @i + 1) as rank,t2.*from (select @i := 0) varcross join ( select s.s_id,s.s_name,avg_score from student s inner join ( select s_id,round(avg(s_score), 2) as avg_score from score group by s_id ) t1 on s.s_id = t1.s_id order by avg_score desc) t2;
+------+------+--------+-----------+
| rank | s_id | s_name | avg_score |
+------+------+--------+-----------+
| 1 | 7 | 郑竹 | 93.50 |
| 2 | 1 | 赵雷 | 89.67 |
| 3 | 5 | 周梅 | 81.50 |
| 4 | 3 | 孙风 | 80.00 |
| 5 | 2 | 钱电 | 65.00 |
| 6 | 4 | 李云 | 40.00 |
| 7 | 6 | 吴兰 | 32.50 |
+------+------+--------+-----------+
方法 1:内外关联
select c.c_id,c.c_name,s.s_id,s.s_name,s_scorefrom ( select * from score sc where ( select count(*) from score sc1 where sc.c_id = sc1.c_id and sc.s_score < sc1.s_score ) < 3)t1inner join student s on t1.s_id = s.s_id inner join course c on t1.c_id = c.c_id order by c.c_id,s_score desc;
+------+--------+------+--------+---------+
| c_id | c_name | s_id | s_name | s_score |
+------+--------+------+--------+---------+
| 1 | 语文 | 1 | 赵雷 | 80 |
| 1 | 语文 | 3 | 孙风 | 80 |
| 1 | 语文 | 5 | 周梅 | 76 |
| 2 | 数学 | 1 | 赵雷 | 90 |
| 2 | 数学 | 7 | 郑竹 | 89 |
| 2 | 数学 | 5 | 周梅 | 87 |
| 3 | 英语 | 1 | 赵雷 | 99 |
| 3 | 英语 | 7 | 郑竹 | 98 |
| 3 | 英语 | 3 | 孙风 | 80 |
+------+--------+------+--------+---------+
方法 2:序号筛选
select *from ( select (@i := case when @pre_group_id = c_id then @i + 1 else 1 end) as rank, (@pre_group_id := c_id) as c_id, c_name,s_id,s_name,s_score from (select @i := 0, @pre_group_id := 1) var cross join ( select c.c_id,c.c_name,s.s_id,s.s_name,s_score from score sc inner join student s on sc.s_id = s.s_id inner join course c on sc.c_id = c.c_id group by c.c_id,s.s_id order by c.c_id,s_score desc ) t1) t2where rank <= 3;
+------+------+--------+------+--------+---------+
| rank | c_id | c_name | s_id | s_name | s_score |
+------+------+--------+------+--------+---------+
| 1 | 1 | 语文 | 1 | 赵雷 | 80 |
| 2 | 1 | 语文 | 3 | 孙风 | 80 |
| 3 | 1 | 语文 | 5 | 周梅 | 76 |
| 1 | 2 | 数学 | 1 | 赵雷 | 90 |
| 2 | 2 | 数学 | 7 | 郑竹 | 89 |
| 3 | 2 | 数学 | 5 | 周梅 | 87 |
| 1 | 3 | 英语 | 1 | 赵雷 | 99 |
| 2 | 3 | 英语 | 7 | 郑竹 | 98 |
| 3 | 3 | 英语 | 3 | 孙风 | 80 |
+------+------+--------+------+--------+---------+
select c.c_id,c.c_name,cnt_studentfrom course cinner join ( select c_id,count(*) as cnt_student from score group by c_id) t1on c.c_id = t1.c_id;
+------+--------+-------------+
| c_id | c_name | cnt_student |
+------+--------+-------------+
| 1 | 语文 | 6 |
| 2 | 数学 | 6 |
| 3 | 英语 | 6 |
+------+--------+-------------+
select s_id,s_namefrom studentwhere s_id in ( select s_id from score group by s_id having count(*) = 2);
+------+--------+
| s_id | s_name |
+------+--------+
| 5 | 周梅 |
| 6 | 吴兰 |
| 7 | 郑竹 |
+------+--------+
select s_sex,count(*) as cnt_sexfrom studentgroup by s_sex;
+-------+---------+
| s_sex | cnt_sex |
+-------+---------+
| 女 | 4 |
| 男 | 4 |
+-------+---------+
select *from studentwhere s_name like '%风%';
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 3 | 孙风 | 1990-05-20 | 男 |
+------+--------+------------+-------+
select *,count(*) as cnt_studentfrom studentgroup by s_name,s_sexhaving cnt_student > 1;
+-----------+
| Empty set |
+-----------+
select *from studentwhere year(s_birth) = '1990';
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 1 | 赵雷 | 1990-01-01 | 男 |
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
| 8 | 王菊 | 1990-01-20 | 女 |
+------+--------+------------+-------+
select c.c_id,c.c_name,avg_scorefrom course cinner join ( select c_id,round(avg(s_score), 2) as avg_score from score group by c_id) t1on c.c_id = t1.c_idorder by avg_score desc,c.c_id;
+------+--------+-----------+
| c_id | c_name | avg_score |
+------+--------+-----------+
| 2 | 数学 | 72.67 |
| 3 | 英语 | 69.33 |
| 1 | 语文 | 64.50 |
+------+--------+-----------+
select s.s_id,s.s_name,avg_scorefrom student sinner join ( select s_id,round(avg(s_score), 2) as avg_score from score group by s_id having avg_score >= 85) t1on s.s_id = t1.s_id;
+------+--------+-----------+
| s_id | s_name | avg_score |
+------+--------+-----------+
| 1 | 赵雷 | 89.67 |
| 7 | 郑竹 | 93.50 |
+------+--------+-----------+
select s_name,s_scorefrom student sinner join ( select s_id,s_score from score sc inner join course c on sc.c_id = c.c_id where c_name = '数学' and s_score < 60) t1on s.s_id = t1.s_id;
+--------+---------+
| s_name | s_score |
+--------+---------+
| 李云 | 30 |
+--------+---------+
select s.s_id,s.s_name, sum(case c_id when 1 then s_score else 0 end) as 语文, sum(case c_id when 2 then s_score else 0 end) as 数学, sum(case c_id when 3 then s_score else 0 end) as 英语from student sleft join score sc on s.s_id = sc.s_idgroup by s.s_id;
+------+--------+------+------+------+
| s_id | s_name | 语文 | 数学 | 英语 |
+------+--------+------+------+------+
| 1 | 赵雷 | 80 | 90 | 99 |
| 2 | 钱电 | 70 | 60 | 65 |
| 3 | 孙风 | 80 | 80 | 80 |
| 4 | 李云 | 50 | 30 | 40 |
| 5 | 周梅 | 76 | 87 | 0 |
| 6 | 吴兰 | 31 | 0 | 34 |
| 7 | 郑竹 | 0 | 89 | 98 |
| 8 | 王菊 | 0 | 0 | 0 |
+------+--------+------+------+------+
select s_name,c_name,s_scorefrom score scinner join student s on sc.s_id = s.s_idinner join course c on sc.c_id = c.c_idwhere s_score > 70group by s.s_id;
+--------+--------+---------+
| s_name | c_name | s_score |
+--------+--------+---------+
| 赵雷 | 语文 | 80 |
| 孙风 | 语文 | 80 |
| 周梅 | 语文 | 76 |
| 郑竹 | 数学 | 89 |
+--------+--------+---------+
select s.s_id,s.s_name,c.c_id,c.c_name,s_scorefrom score scinner join student s on sc.s_id = s.s_idinner join course c on sc.c_id = c.c_idwhere s_score < 60;
+------+--------+------+--------+---------+
| s_id | s_name | c_id | c_name | s_score |
+------+--------+------+--------+---------+
| 4 | 李云 | 1 | 语文 | 50 |
| 4 | 李云 | 2 | 数学 | 30 |
| 4 | 李云 | 3 | 英语 | 40 |
| 6 | 吴兰 | 1 | 语文 | 31 |
| 6 | 吴兰 | 3 | 英语 | 34 |
+------+--------+------+--------+---------+
select s.s_id,s.s_namefrom student swhere s_id in ( select s_id from score where c_id = 1 and s_score >= 80);
+------+--------+
| s_id | s_name |
+------+--------+
| 1 | 赵雷 |
| 3 | 孙风 |
+------+--------+
select c.c_id,c.c_name,cnt_studentfrom course cinner join ( select c_id,count(*) as cnt_student from score group by c_id) t1on c.c_id = t1.c_id;
+------+--------+-------------+
| c_id | c_name | cnt_student |
+------+--------+-------------+
| 1 | 语文 | 6 |
| 2 | 数学 | 6 |
| 3 | 英语 | 6 |
+------+--------+-------------+
select s.*,s_score as max_scorefrom student sinner join ( select s_id,s_score from score sc inner join ( select c_id from course c inner join teacher t on c.t_id = t.t_id where t_name = '张三' ) t1 on sc.c_id = t1.c_id order by s_score desc limit 1) t2on s.s_id = t2.s_id;
+------+--------+------------+-------+-----------+
| s_id | s_name | s_birth | s_sex | max_score |
+------+--------+------------+-------+-----------+
| 1 | 赵雷 | 1990-01-01 | 男 | 90 |
+------+--------+------------+-------+-----------+
select *from scorewhere s_score in ( select s_score from score group by s_score having count(*) > 1);
+------+------+---------+
| s_id | c_id | s_score |
+------+------+---------+
| 1 | 1 | 80 |
| 3 | 1 | 80 |
| 3 | 2 | 80 |
| 3 | 3 | 80 |
+------+------+---------+
方法 1:内外关联
select c.c_id,c.c_name,s.s_id,s.s_name,s_scorefrom ( select * from score sc where ( select count(*) from score sc1 where sc.c_id = sc1.c_id and sc.s_score < sc1.s_score ) < 3) t1inner join student s on t1.s_id = s.s_idinner join course c on t1.c_id = c.c_idorder by c.c_id,s_score desc;
+------+--------+------+--------+---------+
| c_id | c_name | s_id | s_name | s_score |
+------+--------+------+--------+---------+
| 1 | 语文 | 1 | 赵雷 | 80 |
| 1 | 语文 | 3 | 孙风 | 80 |
| 1 | 语文 | 5 | 周梅 | 76 |
| 2 | 数学 | 1 | 赵雷 | 90 |
| 2 | 数学 | 7 | 郑竹 | 89 |
| 2 | 数学 | 5 | 周梅 | 87 |
| 3 | 英语 | 1 | 赵雷 | 99 |
| 3 | 英语 | 7 | 郑竹 | 98 |
| 3 | 英语 | 3 | 孙风 | 80 |
+------+--------+------+--------+---------+
方法 2:序号筛选
select *from ( select (@i := case when @pre_group_id = c_id then @i + 1 else 1 end) as rank, (@pre_group_id := c_id) as c_id, c_name,s_id,s_name,s_score from (select @i := 0, @pre_group_id := 1) var cross join ( select c.c_id,c.c_name,s.s_id,s.s_name,s_score from score sc inner join student s on sc.s_id = s.s_id inner join course c on sc.c_id = c.c_id group by c.c_id,s.s_id order by c.c_id,s_score desc ) t1) t2where rank <= 3;
+------+------+--------+------+--------+---------+
| rank | c_id | c_name | s_id | s_name | s_score |
+------+------+--------+------+--------+---------+
| 1 | 1 | 语文 | 1 | 赵雷 | 80 |
| 2 | 1 | 语文 | 3 | 孙风 | 80 |
| 3 | 1 | 语文 | 5 | 周梅 | 76 |
| 1 | 2 | 数学 | 1 | 赵雷 | 90 |
| 2 | 2 | 数学 | 7 | 郑竹 | 89 |
| 3 | 2 | 数学 | 5 | 周梅 | 87 |
| 1 | 3 | 英语 | 1 | 赵雷 | 99 |
| 2 | 3 | 英语 | 7 | 郑竹 | 98 |
| 3 | 3 | 英语 | 3 | 孙风 | 80 |
+------+------+--------+------+--------+---------+
– 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select c.c_id,cnt_studentfrom course cinner join ( select c_id,count(*) as cnt_student from score group by c_id having cnt_student > 5) t1on c.c_id = t1.c_idorder by cnt_student desc,c.c_id;
+------+-------------+
| c_id | cnt_student |
+------+-------------+
| 1 | 6 |
| 2 | 6 |
| 3 | 6 |
+------+-------------+
select s_idfrom scoregroup by s_idhaving count(*) >= 2;
+------+
| s_id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+------+
查询选修了全部课程的学生信息
select *from studentwhere s_id in ( select s_id from score group by s_id having count(*) = ( select count(*) from course ));
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 1 | 赵雷 | 1990-01-01 | 男 |
| 2 | 钱电 | 1990-12-21 | 男 |
| 3 | 孙风 | 1990-05-20 | 男 |
| 4 | 李云 | 1990-08-06 | 男 |
+------+--------+------------+-------+
– 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select s_id,s_name,s_birth, if ( month(current_date()) < month(s_birth) or (month(current_date()) = month(s_birth) and day(current_date()) < day(s_birth)), year(current_date()) - year(s_birth) -1, year(current_date()) - year(s_birth) ) as s_agefrom student;
select *from studentwhere datediff(concat(year(current_date()), date_fORMat(s_birth, '-%m-%d')), current_date()) between 0 and 7or datediff(concat(year(current_date()) + 1, date_format(s_birth, '-%m-%d')), current_date()) between 0 and 7;
select *from studentwhere datediff(concat(year(current_date()), date_format(s_birth, '-%m-%d')), current_date()) between 7 and 14or datediff(concat(year(current_date()) + 1, date_format(s_birth, '-%m-%d')), current_date()) between 7 and 14;
查询本月过生日的学生
select *from studentwhere month(s_birth) = month(current_date());
查询12月份过生日的学生
select *from studentwhere month(s_birth) = 12;
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 2 | 钱电 | 1990-12-21 | 男 |
| 5 | 周梅 | 1991-12-01 | 女 |
+------+--------+------------+-------+
来源地址:https://blog.csdn.net/AL_K_ZN/article/details/131549291
--结束END--
本文标题: MySQL 经典练习 50 题(完美解答版)
本文链接: https://lsjlt.com/news/409670.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0