20000 字干货笔记,一天搞定 MySQL( 三 )


只有score关联学生的no,因此只要查询score表,就能找出所有和学生相关的no和degree:
 SELECT s_no, c_no, degree FROM score;+------+-------+--------+| s_no | c_no | degree |+------+-------+--------+| 103 | 3-105 | 92 || 103 | 3-245 | 86 || 103 | 6-166 | 85 || 105 | 3-105 | 88 || 105 | 3-245 | 75 || 105 | 6-166 | 79 || 109 | 3-105 | 76 || 109 | 3-245 | 68 || 109 | 6-166 | 81 |+------+-------+--------+然后查询course表:
SELECT no, name FROM course;+-------+-----------------+| no | name |+-------+-----------------+| 3-105 | 计算机导论 || 3-245 | 操作系统 || 6-166 | 数字电路 || 9-888 | 高等数学 |+-------+-----------------+只要把score表中的c_no替换成course表中对应的name字段值就可以了 。
 -- 增加一个查询字段 name,分别从 score、course 这两个表中查询 。-- as 表示取一个该字段的别名 。SELECT s_no, name as c_name, degree FROM score, courseWHERE score.c_no = course.no;+------+-----------------+--------+| s_no | c_name | degree |+------+-----------------+--------+| 103 | 计算机导论 | 92 || 105 | 计算机导论 | 88 || 109 | 计算机导论 | 76 || 103 | 操作系统 | 86 || 105 | 操作系统 | 75 || 109 | 操作系统 | 68 || 103 | 数字电路 | 85 || 105 | 数字电路 | 79 || 109 | 数字电路 | 81 |+------+-----------------+--------+

  • 三表关联查询
查询所有学生的 name 、课程名 ( course 表中的 name ) 和 degree。
只有score表中关联学生的学号和课堂号,我们只要围绕着score这张表查询就好了 。
SELECT * FROM score;+------+-------+--------+| s_no | c_no | degree |+------+-------+--------+| 103 | 3-105 | 92 || 103 | 3-245 | 86 || 103 | 6-166 | 85 || 105 | 3-105 | 88 || 105 | 3-245 | 75 || 105 | 6-166 | 79 || 109 | 3-105 | 76 || 109 | 3-245 | 68 || 109 | 6-166 | 81 |+------+-------+--------+只要把 s_no 和 c_no 替换成 student 和 course 表中对应的 name 字段值就好了 。
首先把 s_no 替换成 student 表中的 name 字段:
SELECT name, c_no, degree FROM student, score WHERE student.no = score.s_no;+-----------+-------+--------+| name | c_no | degree |+-----------+-------+--------+| 王丽 | 3-105 | 92 || 王丽 | 3-245 | 86 || 王丽 | 6-166 | 85 || 王芳 | 3-105 | 88 || 王芳 | 3-245 | 75 || 王芳 | 6-166 | 79 || 赵铁柱 | 3-105 | 76 || 赵铁柱 | 3-245 | 68 || 赵铁柱 | 6-166 | 81 |+-----------+-------+--------+再把c_no替换成course表中的name字段:
-- 课程表SELECT no, name FROM course;+-------+-----------------+| no | name |+-------+-----------------+| 3-105 | 计算机导论 || 3-245 | 操作系统 || 6-166 | 数字电路 || 9-888 | 高等数学 |+-------+-----------------+-- 由于字段名存在重复,使用 "表名.字段名 as 别名" 代替 。SELECT student.name as s_name, course.name as c_name, degreeFROM student, score, courseWHERE student.NO = score.s_noAND score.c_no = course.no;
  • 子查询加分组求平均分
查询95031班学生每门课程的平均成绩 。
在score表中根据student表的学生编号筛选出学生的课堂号和成绩:
-- IN (..): 将筛选出的学生号当做 s_no 的条件查询SELECT s_no, c_no, degree FROM scoreWHERE s_no IN (SELECT no FROM student WHERE class = '95031');+------+-------+--------+| s_no | c_no | degree |+------+-------+--------+| 105 | 3-105 | 88 || 105 | 3-245 | 75 || 105 | 6-166 | 79 || 109 | 3-105 | 76 || 109 | 3-245 | 68 || 109 | 6-166 | 81 |+------+-------+--------+这时只要将c_no分组一下就能得出95031班学生每门课的平均成绩:
SELECT c_no, AVG(degree) FROM scoreWHERE s_no IN (SELECT no FROM student WHERE class = '95031')GROUP BY c_no;+-------+-------------+| c_no | AVG(degree) |+-------+-------------+| 3-105 | 82.0000 || 3-245 | 71.5000 || 6-166 | 80.0000 |+-------+-------------+
  • 子查询 - 1
查询在 3-105 课程中,所有成绩高于 109 号同学的记录 。
先用子查询查找出109同学在3-105中的成绩
select * from scorewhere c_no ='3-105' and s_no='109'然后再以课程3-105为条件,查找成绩大76的记录
select * from scorewhere c_no = '3-105'and degree>(select degree from scorewhere c_no = '3-105' and s_no='109');
  • 子查询 - 2
查询所有成绩高于 109 号同学的 3-105 课程成绩记录 。
-- 不限制课程号,只要成绩大于109号同学的3-105课程成绩就可以 。SELECT * FROM scoreWHERE degree > (SELECT degree FROM score WHERE s_no = '109' AND c_no = '3-105');
  • YEAR 函数与带 IN 关键字查询
查询所有和 101 、108 号学生同年出生的 no 、name 、birthday 列 。
select no, name, birthday from studentwhere year(birthday) in (select year(birthday) from studentwhere no in (101,108));


推荐阅读