MySQL学习笔记(实操)(7)

mysq查询操作实操:(本节使用创建的表以及架构在文章底部展示)

题目列表:
|| 查询score表中最高分同学的id号,班级与成绩

/*法一:子查询*/
select student_id, course_id,degree from score 
where degree=(select max(degree) from score);
/**法二,排序*/
select student_id, course_id,degree from score 
order by degree desc 
limit 0,1;//limit 第一个数字为起始位置,第二个为限制行数

|| 查询每门课的平均成绩

/*分组操作*/
select course_id,AVG(degree) from score 
group by course_id;

|| 查询score表中至少有2名学生参加并且以3开头的课程的平均分数

/*法一:分完组后再筛选*/
select course_id, AVG(degree)
from score group by course_id 
having count(*) >= 2 and course_id like '3%';

/*法二,部分过滤后再筛选*/
select course_id, AVG(degree) from score 
where  course_id like '3%' 
group by course_id 
having count(*) >= 2;
//注意:统计每门课的学生个数必须再分完组后才可以确定

|| 查询分数大于70,小于90的学生学号

/*法一,and范围查询*/
select student_id,degree from score 
where degree > 70 and degree < 90;
/*法二,between范围查询*/
select student_id,degree from score 
where degree between 71 and 89; 
//注意:between的范围包括边界 [71,89]

|| 查询95031班每个学生每门课的成绩

/*嵌套分组*/
select *,AVG(degree) as '平均分' 
from score 
where student_id in (select student_id from students where class='95031')
group by student_id, course_id;

|| 查询所有学生的名字,课程和分数列

/*多表查询,与多表间的筛选*/
select student_name, course_id, degree  
from students, score  
where students.student_id = score.student_id;

|| 查询95031班所有学生每门课的成绩

/*法一:因为所需字段全部在score表中存在,故围绕score做子查询*/
select * from score 
where student_id in (select student_id from students where class='95031');
/*法二:使用多表查询*/
select ,class,course_id, degree 
from students,score 
where class='95031' and students.student_id = score.student_id;

|| 这道题的思考:多表查询时,多关注多表间相同的列

|| 查询有高于85分存在的课程

select * from course 
where course_id in (select course_id from score where degree >= 85);

|| 查询在‘3-105’课程上成绩高于‘109’号学生的所有学生的记录

/*子查询中镶嵌子查询*/
select * from students
where student_id in(
select student_id from score where course_id='3-105' and
degree>(select degree from score where course_id = '3-105' and student_id ='109')
);

|| 查询老师张旭的学生的成绩

/*子查询多层嵌套*/
select * from score 
where course_id=(
select course_id from course where teacher_id =(
select teacher_id from teacher where teacher_name =' 张旭')
);

|| 注意:多表间的同名字段(外键) 是 表与表之间的桥梁

|| 查询计算机系老师所教课程的成绩表

select * from score 
where course_id in (
select course_id from course where teacher_id in (select teacher_id from teacher where teacher_depart='计算机系')
);

|| 查询与学号为108,101同学同年出生的所有同学的学号名字生日列

/*year的使用*/
select student_id, student_name, student_birthday from students 
where year(student_birthday) in (
select year(student_birthday) from students where student_id='108' or student_id ='101'
);

|| 返回3-105课程中,成绩比3-245课程最低成绩高的 同学的信息,并以3-105课程成绩由高到低排序

/*注意:返回多个值的子查询中 与 另一个返回多个值的子查询 无法进行比较*/
/*错误写法*/
select * from score
where any(select degree from score where course_id='3-105') >
any(select degree from score where course_id='3-245');

/*正确写法: any all使得子查询返回的多个值选择其一*/
select * from score 
where course_id = '3-105' and degree>any(select degree from score where course_id = '3-245') 
order by degree desc;

|| 查询拥有一个系中独有职称的老师的名字和职称

/*对于两个查询结果的整合:union*/
select * from teacher 
where teacher_depart = '计算机系' and teacher_rof not in (select teacher_rof from teacher where teacher_depart='电子工程系')
union 
select * from teacher 
where teacher_depart = '电子工程系' and teacher_rof not in (select teacher_rof from teacher where teacher_depart='计算机系');

|| 查询 在‘3-105’课程考得比‘3-245’号课程好 的同学的信息,并按照成绩的3-105考试的成绩从高到低排序

select * from students 
where student_id = 
(select distinct student_id from score where student_id = '103' and 
(select degree from score where course_id = '3-105' and student_id='103')>(select degree from score where course_id = '3-245' and student_id = '103')) 
union  
select * from students
where student_id = (select distinct student_id from score where student_id = '105' and 
(select degree from score where course_id = '3-105' and student_id='103')>(select degree from score where course_id = '3-245' and student_id = '105')) 
union  
select * from students
where student_id = (select distinct student_id from score where student_id = '103' and 
(select degree from score where course_id = '3-105' and student_id='109')>(select degree from score where course_id = '3-245' and student_id = '109'));

|| 2月23日改进补充上一题:(使用自联结)

select distinct student_id from score a 
where 
(select degree from score b where course_id='3-105' and a.student_id = b.student_id)>
(select degree from score b where course_id='3-245' and a.student_id = b.student_id)
;

|| 查询每个科目成绩比该科目平均成绩低的同学

自联结带来的意义在于:能把一个表中的一项记录与该表的其余记录或其余记录的汇总一一作比较
注意:分组后汇总数据返回的是一个值表; 复制表后精确对应汇总返回的是一个值,当使用聚集函数出现返回行数不匹配报错时,立刻使用后者就完事了

/*法一,局限性大很呆*/
select * from score where  course_id = '3-105' and degree < (select AVG(degree) from score group by course_id having course_id = '3-105')
union
select * from score where  course_id = '3-245' and degree < (select AVG(degree) from score group by course_id having course_id = '3-245')
union
select * from score where  course_id = '6-166' and degree < (select AVG(degree) from score group by course_id having course_id = '6-166');


/*使用复制表操作*/
select * from score a 
where degree < (select AVG(degree) from score b where a.course_id = b.course_id);

|| 查询至少有两个男生的班级号

/*错误写法*/
select class from students
where (select count(*) from students where student_sex ='男' group by class) > 2;

/*正确写法*/
select distinct class from students a 
where (select count(*) from students b where a.class = b.class and student_sex='男');

|| 查询非王姓的同学

select * from students where student_name not like '王%';

|| 查询每个学生的姓名与年龄

select student_name, year(now())-year(student_birthday) from students;

|| 查询年龄最小和最大的学生的信息

注意:数值处理函数并不像其他编程语言一样直接返回一个确切的值,它返回的是具有确切值的记录!
典型的错误:select * from students a where student_birthday = max(student_birthday);
正确:select * from students where student_birthday= (select max(student_birthday) from students )

select * from students 
where student_birthday = (select max(student_birthday)from students)
union
select * from students
where student_birthday = (select min(student_birthday)from students);

|| 以班号和年龄大小的顺序从大到小查询学生表

select * from students
order by class desc, student_birthday desc;

|| 查询所有参加计算机课 的男同学

select * from students
where student_id in (
select student_id from score where course_id = (
select course_id from course where course_name='计算机导论'));

|| 创建一个成绩等级表,查询所有同学的学号,课程号和成绩等级

+------+------+-------+
| low  | up   | grade |
+------+------+-------+
|   90 |  100 | A     |
|   80 |   89 | B     |
|   70 |   79 | C     |
|   60 |   69 | D     |
|    0 |   59 | X     |
+------+------+-------+

select student_id, course_id, grade 
from score,grade 
where score.degree>grade.low and degree <grade.up;

文章创建的表以及架构

SHOW TABLES;
+----------------------+	
| Tables_in_selecttest |
+----------------------+	
| course               |	
| score                |	
| student              |
| teacher              |
+----------------------+

 DESCRIBE student;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| s_no       | varchar(20) | NO   | PRI | NULL    |       |
| s_name     | varchar(20) | NO   |     | NULL    |       |
| s_sex      | varchar(10) | NO   |     | NULL    |       |
| s_birthday | datetime    | YES  |     | NULL    |       |
| s_class    | varchar(20) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+

查看teacher表结构    DESCRIBE teacher;

+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| t_no       | varchar(20) | NO   | PRI | NULL    |       |
| t_name     | varchar(20) | NO   |     | NULL    |       |
| t_sex      | varchar(20) | NO   |     | NULL    |       |
| t_birthday | datetime    | YES  |     | NULL    |       |
| t_rof      | varchar(20) | NO   |     | NULL    |       |
| t_depart   | varchar(20) | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
查看course表结构    DESCRIBE course;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |	
+--------+-------------+------+-----+---------+-------+
| c_no   | varchar(20) | NO   | PRI | NULL    |       |
| c_name | varchar(20) | NO   |     | NULL    |       |
| t_no   | varchar(20) | NO   | MUL | NULL    |       |
+--------+-------------+------+-----+---------+-------+
查看score表结构    DESCRIBE score;
+-----------+---------------+------+-----+---------+-------+
| Field     | Type          | Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+-------+
| s_no      | varchar(20)   | NO   | PRI | NULL    |       |
| c_no      | varchar(20)   | NO   | PRI | NULL    |       |
| sc_degree | decimal(10,0) | YES  |     | NULL    |       |
+-----------+---------------+------+-----+---------+-------+
发布了49 篇原创文章 · 获赞 77 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/a13352912632/article/details/104442353