select stu_id, stu_name, stu_addr from tb_student;
select `stu_id`, `stu_name`, `stu_addr` from `tb_student`;
-- 查询所有课程的名称及学分(投影和别名)
-- 查询所有课程的名称及学分(投影和别名)
select cou_name as 课程名称, cou_credit as 学分 from tb_course;
select `cou_name` as 课程名称, `cou_credit` as 学分 from `tb_course`;
-- 查询所有女学生的姓名和出生日期(筛选)
-- 查询所有女学生的姓名和出生日期(筛选)
select stu_name, stu_birth from tb_student where stu_sex=0;
select `stu_name`, `stu_birth` from `tb_student` where `stu_sex`=0;
-- 查询籍贯为“四川成都”的女学生的姓名和出生日期(筛选)
select `stu_name`, `stu_birth` from `tb_student` where `stu_sex`=0 and `stu_addr`='四川成都';
-- 查询籍贯为“四川成都”或者性别为“女生”的学生
select `stu_name`, `stu_birth` from `tb_student` where `stu_sex`=0 or `stu_addr`='四川成都';
-- 查询所有80后学生的姓名、性别和出生日期(筛选)
-- 查询所有80后学生的姓名、性别和出生日期(筛选)
select stu_name, stu_sex, stu_birth from tb_student where stu_birth>='1980-1-1' and stu_birth<='1989-12-31';
select `stu_name`, `stu_sex`, `stu_birth` from `tb_student`
where `stu_birth`>='1980-1-1' and `stu_birth`<='1989-12-31';
select stu_name, stu_sex, stu_birth from tb_student where stu_birth between '1980-1-1' and '1989-12-31';
select `stu_name`, `stu_sex`, `stu_birth` from `tb_student`
-- 将表示性别的 1 和 0 处理成 “男” 和 “女”
where `stu_birth` between '1980-1-1' and '1989-12-31';
select stu_name as 姓名, case stu_sex when 1 then '男' else '女' end as 性别, stu_birth as 生日 from tb_student where stu_birth between '1980-1-1' and '1989-12-31';
-- 补充:将表示性别的 1 和 0 处理成 “男” 和 “女”
select
`stu_name` as 姓名,
if(`stu_sex`, '男', '女') as 性别,
`stu_birth` as 出生日期
from `tb_student`
where `stu_birth` between '1980-1-1' and '1989-12-31';
select
`stu_name` as 姓名,
case `stu_sex` when 1 then '男' else '女' end as 性别,
`stu_birth` as 出生日期
from `tb_student`
where `stu_birth` between '1980-1-1' and '1989-12-31';
select stu_name as 姓名, if(stu_sex, '男', '女') as 性别, stu_birth as 生日 from tb_student where stu_birth between '1980-1-1' and '1989-12-31';
-- 查询学分大于2的课程的名称和学分(筛选)
select `cou_name`, `cou_credit` from `tb_course` where `cou_credit`>2;
-- 查询学分是奇数的课程的名称和学分(筛选)
select `cou_name`, `cou_credit` from `tb_course` where `cou_credit`%2<>0;
select `cou_name`, `cou_credit` from `tb_course` where `cou_credit` mod 2<>0;
-- 查询选择选了1111的课程考试成绩在90分以上的学生学号(筛选)
select `stu_id` from `tb_record` where `cou_id`=1111 and `score`>90;
-- 查询名字叫“杨过”的学生的姓名和性别
select `stu_name`, `stu_sex` from `tb_student` where `stu_name`='杨过';
-- 查询姓“杨”的学生姓名和性别(模糊)
-- 查询姓“杨”的学生姓名和性别(模糊)
select stu_name, stu_sex from tb_student where stu_name like '杨%';
-- % - 通配符(wildcard),它可以匹配0个或任意多个字符
select `stu_name`, `stu_sex` from `tb_student` where `stu_name` like '杨%';
-- 查询姓“杨”名字两个字的学生姓名和性别(模糊)
-- 查询姓“杨”名字两个字的学生姓名和性别(模糊)
select stu_name, stu_sex from tb_student where stu_name like '杨_';
-- _ - 通配符(wildcard),它可以精确匹配一个字符
select `stu_name`, `stu_sex` from `tb_student` where `stu_name` like '杨_';
-- 查询姓“杨”名字三个字的学生姓名和性别(模糊)
-- 查询姓“杨”名字三个字的学生姓名和性别(模糊)
select stu_name, stu_sex from tb_student where stu_name like '杨__';
select `stu_name`, `stu_sex` from `tb_student` where `stu_name` like '杨__';
-- 查询名字中有“不”字或“嫣”字的学生的姓名(模糊)
-- 查询名字中有“不”字或“嫣”字的学生的姓名(模糊)
select stu_name from tb_student where stu_name like '%不%' or stu_name like '%嫣%';
select `stu_name` from `tb_student` where `stu_name` like '%不%' or `stu_name` like '%嫣%';
-- 将“岳不群”改名为“岳不嫣”,比较下面两个查询的区别
update `tb_student` set `stu_name`='岳不嫣' where `stu_id`=1572;
select stu_name from tb_student where stu_name like '%嫣%'
select `stu_name` from `tb_student` where `stu_name` like '%不%'
union
union
select stu_name from tb_student where stu_name like '%不%';
select `stu_name` from `tb_student` where `stu_name` like '%嫣%';
select `stu_name` from `tb_student` where `stu_name` like '%不%'
union all
select `stu_name` from `tb_student` where `stu_name` like '%嫣%';
-- 查询姓“杨”或姓“林”名字三个字的学生的姓名(正则表达式模糊查询)
-- 查询姓“杨”或姓“林”名字三个字的学生的姓名(正则表达式模糊查询)
select stu_name from tb_student where stu_name regexp '[杨林].{2}';
select `stu_name` from `tb_student` where `stu_name` regexp '[杨林].{2}';
-- 查询没有录入籍贯的学生姓名(空值处理)
select `stu_name` from `tb_student` where `stu_addr` is null;
-- 查询没有录入家庭住址的学生姓名(空值)
select `stu_name` from `tb_student` where `stu_addr` <=> null;
select stu_name from tb_student where stu_addr is null or stu_add='';
select stu_name from tb_student where stu_addr<=>null or stu_addr='';
-- 查询录入了籍贯的学生姓名(空值处理)
select `stu_name` from `tb_student` where `stu_addr` is not null;
-- 查询录入了家庭住址的学生姓名(空值)
-- 下面的查询什么也查不到,三值逻辑 --> true / false / unknown
select stu_name from tb_student where stu_addr is not null and stu_addr<>'';
select `stu_name` from `tb_student` where `stu_addr`=null or `stu_addr`<>null;
-- 查询学生选课的所有日期(去重)
-- 查询学生选课的所有日期(去重)
select distinct sel_date from tb_record;
select distinct `sel_date` from `tb_record`;
-- 查询学生的家庭住址(去重)
-- 查询学生的籍贯(去重)
select distinct stu_addr from tb_student where stu_addr is not null;
select distinct `stu_addr` from `tb_student` where `stu_addr` is not null;
-- 查询男学生的姓名和生日按年龄从大到小排列(排序)
-- 查询男学生的姓名和生日按年龄从大到小排列(排序)
select stu_name, stu_birth from tb_student where stu_sex=1 order by stu_birth asc;
-- 升序:从小到大 - asc,降序:从大到小 - desc
select `stu_id`, `stu_name`, `stu_birth` from `tb_student`
-- 将生日换算成年龄(日期函数、数值函数)
where `stu_sex`=1 order by `stu_birth` asc, `stu_id` desc;
select stu_name, stu_birth, floor(datediff(curdate(), stu_birth)/365) as stu_age from tb_student where stu_sex=1 order by stu_age desc;
-- 补充:将上面的生日换算成年龄(日期函数、数值函数)
select
`stu_id` as 学号,
`stu_name` as 姓名,
floor(datediff(curdate(), `stu_birth`)/365) as 年龄
from `tb_student`
where `stu_sex`=1 order by 年龄 desc, `stu_id` desc;
-- 查询年龄最大的学生的出生日期(聚合函数)
-- 查询年龄最大的学生的出生日期(聚合函数)
select min(stu_birth) from tb_student;
select min(`stu_birth`) from `tb_student`;
-- 查询年龄最小的学生的出生日期(聚合函数)
-- 查询年龄最小的学生的出生日期(聚合函数)
select max(stu_birth) from tb_student;
select max(`stu_birth`) from `tb_student`;
-- 查询编号为1111的课程考试成绩的最高分
-- 查询编号为1111的课程考试成绩的最高分(聚合函数)
select max(score) from tb_record where cou_id=1111;
select max(`score`) from `tb_record` where `cou_id`=1111;
-- 查询学号为1001的学生考试成绩的最低分
-- 查询学号为1001的学生考试成绩的最低分(聚合函数)
select min(score) from tb_record where stu_id=1001;
select min(`score`) from `tb_record` where `stu_id`=1001;
-- 查询学号为1001的学生考试成绩的平均分
-- 查询学号为1001的学生考试成绩的平均分(聚合函数)
select avg(score) from tb_record where stu_id=1001;
select avg(`score`) from `tb_record` where `stu_id`=1001;
select sum(score) / count(score) from tb_record where stu_id=1001;
-- 查询学号为1001的学生考试成绩的平均分,如果有null值,null值算0分
select sum(`score`) / count(`score`) from `tb_record` where `stu_id`=1001;
select sum(score) / count(*) from tb_record where stu_id=1001;
select avg(ifnull(score, 0)) from tb_record where stu_id=1001;
-- 查询学号为1001的学生考试成绩的标准差
-- 查询学号为1001的学生考试成绩的平均分,如果有null值,null值算0分(聚合函数)
select std(score) from tb_record where stu_id=1001;
select sum(`score`) / count(*) from `tb_record` where `stu_id`=1001;
-- 查询男女学生的人数(分组和聚合函数)
select avg(ifnull(`score`, 0)) from `tb_record` where `stu_id`=1001;
select if(stu_sex, '男', '女') as 性别, count(*) as 人数 from tb_student group by stu_sex;
-- 查询学号为1001的学生考试成绩的标准差(聚合函数)
select std(`score`), variance(`score`) from `tb_record` where `stu_id`=1001;
-- 查询每个学院男女学生人数
-- 查询男女学生的人数(分组和聚合函数)
select col_id as 学院编号, if(stu_sex, '男', '女') as 性别, count(*) as 人数 from tb_student group by col_id, stu_sex;
select
case `stu_sex` when 1 then '男' else '女' end as 性别,
count(*) as 人数
from `tb_student` group by `stu_sex`;
-- 查询每个学院学生人数(分组和聚合函数)
select
`col_id` as 学院,
count(*) as 人数
from `tb_student` group by `col_id` with rollup;
-- 查询每个学院男女学生人数(分组和聚合函数)
select
`col_id` as 学院,
if(`stu_sex`, '男', '女') as 性别,
count(*) as 人数
from `tb_student` group by `col_id`, `stu_sex`;
-- 查询每个学生的学号和平均成绩(分组和聚合函数)
-- 查询每个学生的学号和平均成绩(分组和聚合函数)
select stu_id as 学号, round(avg(score), 2) as 平均分 from tb_record group by stu_id;
select
`stu_id`,
round(avg(`score`), 1) as avg_score
from `tb_record` group by `stu_id`;
-- 查询平均成绩大于等于90分的学生的学号和平均成绩
-- 查询平均成绩大于等于90分的学生的学号和平均成绩
select stu_id as 学号, round(avg(score), 2) as 平均分 from tb_record group by stu_id having 平均分>=90;
-- 分组以前的筛选使用where子句,分组以后的筛选使用having子句
select
`stu_id`,
round(avg(`score`), 1) as avg_score
from `tb_record`
group by `stu_id` having avg_score>=90;
-- 查询1111、2222、3333三门课程平均成绩大于等于90分的学生的学号和平均成绩
-- 查询1111、2222、3333三门课程平均成绩大于等于90分的学生的学号和平均成绩
select stu_id as 学号, round(avg(score), 2) as 平均分 from tb_record where cou_id in (1111, 2222, 3333) group by stu_id having 平均分>=90;
select
`stu_id`,
-- 查询年龄最大的学生的姓名(子查询)
round(avg(`score`), 1) as avg_score
select stu_name from tb_student where stu_birth=(select min(stu_birth) from tb_student);
from `tb_record` where `cou_id` in (1111, 2222, 3333)
group by `stu_id` having avg_score>=90;
-- 查询年龄最大的学生的姓名(子查询/嵌套查询)
-- 嵌套查询:把一个select的结果作为另一个select的一部分来使用
select `stu_name` from `tb_student`
where `stu_birth`=(
select min(`stu_birth`) from `tb_student`
);
-- 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算)
-- 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算)
select stu_name from tb_student where stu_id in (select stu_id from tb_record group by stu_id having count(*)>2);
select `stu_name` from `tb_student`
where `stu_id` in (
select `stu_id` from `tb_record`
group by `stu_id` having count(*)>2
);
-- 查询学生的姓名、生日和所在学院名称
-- 查询学生的姓名、生日和所在学院名称
select stu_name, stu_birth, col_name from tb_student, tb_college where tb_student.col_id=tb_college.col_id;
select `stu_name`, `stu_birth`, `col_name`
from `tb_student`, `tb_college`
where `tb_student`.`col_id`=`tb_college`.`col_id`;
select stu_name, stu_birth, col_name from tb_student t1 inner join tb_college t2 on t1.col_id=t2.col_id;
select `stu_name`, `stu_birth`, `col_name`
from `tb_student` inner join `tb_college`
on `tb_student`.`col_id`=`tb_college`.`col_id`;
select stu_name, stu_birth, col_name from tb_student natural join tb_college;
select `stu_name`, `stu_birth`, `col_name`
from `tb_student` natural join `tb_college`;
-- 查询学生姓名、课程名称以及成绩(连接查询/联结查询)
-- 查询学生姓名、课程名称以及成绩(连接查询/联结查询)
select t2.stu_id, stu_name, t3.cou_id, cou_name, score from tb_record t1, tb_student t2, tb_course t3 where t1.stu_id=t2.stu_id and t1.cou_id=t3.cou_id and score is not null;
select `stu_name`, `cou_name`, `score`
from `tb_student`, `tb_course`, `tb_record`
select stu_name, cou_name, score from tb_student t1 inner join tb_record t2 on t1.stu_id=t2.stu_id inner join tb_course t3 on t2.cou_id=t3.cou_id where score is not null;
where `tb_student`.`stu_id`=`tb_record`.`stu_id`
and `tb_course`.`cou_id`=`tb_record`.`cou_id`
select stu_name, cou_name, score from tb_student natural join tb_record natural join tb_course where score is not null;
and `score` is not null;
-- 分页查询(前5条数据)
select `stu_name`, `cou_name`, `score` from `tb_student`
select stu_name, cou_name, score from tb_student natural join tb_record natural join tb_course where score is not null order by score desc limit 5;
inner join `tb_record` on `tb_student`.`stu_id`=`tb_record`.`stu_id`
inner join `tb_course` on `tb_course`.`cou_id`=`tb_record`.`cou_id`
-- 分页查询(6-10条数据)
where `score` is not null;
select stu_name, cou_name, score from tb_student natural join tb_record natural join tb_course where score is not null order by score desc limit 5 offset 5;
select `stu_name`, `cou_name`, `score` from `tb_student`
-- 分页查询(11-15条数据)
natural join `tb_record`
select stu_name, cou_name, score from tb_student natural join tb_record natural join tb_course where score is not null order by score desc limit 5 offset 10;
natural join `tb_course`
where `score` is not null;
select stu_name, cou_name, score from tb_student natural join tb_record natural join tb_course where score is not null order by score desc limit 10,5;
-- 补充:上面的查询结果取前5条数据(分页查询)
select `stu_name`, `cou_name`, `score`
from `tb_student`, `tb_course`, `tb_record`
where `tb_student`.`stu_id`=`tb_record`.`stu_id`
and `tb_course`.`cou_id`=`tb_record`.`cou_id`
and `score` is not null
order by `score` desc
limit 0,5;
-- 补充:上面的查询结果取第6-10条数据(分页查询)
select `stu_name`, `cou_name`, `score`
from `tb_student`, `tb_course`, `tb_record`
where `tb_student`.`stu_id`=`tb_record`.`stu_id`
and `tb_course`.`cou_id`=`tb_record`.`cou_id`
and `score` is not null
order by `score` desc
limit 5 offset 5;
-- 补充:上面的查询结果取第11-15条数据(分页查询)
select `stu_name`, `cou_name`, `score`
from `tb_student`, `tb_course`, `tb_record`
where `tb_student`.`stu_id`=`tb_record`.`stu_id`
and `tb_course`.`cou_id`=`tb_record`.`cou_id`
and `score` is not null
order by `score` desc
limit 5 offset 10;
-- 查询选课学生的姓名和平均成绩(子查询和连接查询)
-- 查询选课学生的姓名和平均成绩(子查询和连接查询)
select stu_name, avg_score from tb_student t1, (select stu_id, round(avg(score),1) as avg_score from tb_record group by stu_id) t2 where t1.stu_id=t2.stu_id;
select `stu_name`, `avg_score`
from `tb_student` inner join (
select `stu_id` as `sid`, round(avg(`score`), 1) as avg_score
from `tb_record` group by `stu_id`
) as `t2` on `stu_id`=`sid`;
-- 查询学生的姓名和选课的数量
-- 查询学生的姓名和选课的数量
select stu_name, total from tb_student t1, (select stu_id, count(*) as total from tb_record group by stu_id) t2 where t1.stu_id=t2.stu_id;
select `stu_name`, `total` from `tb_student` as `t1`
inner join (
select `stu_id`, count(*) as `total`
from `tb_record` group by `stu_id`
) as `t2` on `t1`.`stu_id`=`t2`.`stu_id`;
-- 查询每个学生的姓名和选课数量(左外连接和子查询)
-- 查询每个学生的姓名和选课数量(左外连接和子查询)
select stu_name, ifnull(total, 0) as total from tb_student t1 left outer join (select stu_id, count(*) as total from tb_record group by stu_id) t2 on t1.stu_id=t2.stu_id;
select `stu_name`, coalesce(`total`, 0) as `total`
-- 删除tb_record表的外键约束
from `tb_student` as `t1`
alter table tb_record drop foreign key fk_record_stu_id;
left outer join (
alter table tb_record drop foreign key fk_record_cou_id;
select `stu_id`, count(*) as `total`
from `tb_record` group by `stu_id`
-- 给tb_record表加两条记录,学号5566在学生表没有对应的记录
) as `t2` on `t1`.`stu_id`=`t2`.`stu_id`;
insert into tb_record
-- 修改选课记录表,去掉 stu_id 列的外键约束
alter table `tb_record` drop foreign key `fk_record_stu_id`;
-- 插入两条新纪录(注意:没有学号为 5566 的学生)
insert into `tb_record`
values
values
(default, 5566, 1111, '2019-09-02', 80),
(default, 5566, 1111, '2019-09-02', 80),
(default, 5566, 2222, '2019-09-02', 70);
(default, 5566, 2222, '2019-09-02', 70);
-- 查询学生的姓名和选课数量(右外连接)
select t1.stu_id, stu_name, t2.stu_id, total as total from tb_student t1 right outer join (select stu_id, count(*) as total from tb_record group by stu_id) t2 on t1.stu_id=t2.stu_id;
select t1.stu_id, stu_name, t2.stu_id, total as total from tb_student t1 left outer join (select stu_id, count(*) as total from tb_record group by stu_id) t2 on t1.stu_id=t2.stu_id
select `stu_name`, `total` from `tb_student` as `t1`
right outer join (
select `stu_id`, count(*) as `total`
from `tb_record` group by `stu_id`
) as `t2` on `t1`.`stu_id`=`t2`.`stu_id`;
-- 全外连接:左表和右表的每条记录都可以查出来,不满足连表条件的地方填充null。
-- 说明:MySQL不支持全外连接,所以用左外连接和右外连接的并集来表示。
select `stu_name`, `total`
from `tb_student` as `t1`
left outer join (
select `stu_id`, count(*) as `total`
from `tb_record` group by `stu_id`
) as `t2` on `t1`.`stu_id`=`t2`.`stu_id`
union
union
select t1.stu_id, stu_name, t2.stu_id, total as total from tb_student t1 right outer join (select stu_id, count(*) as total from tb_record group by stu_id) t2 on t1.stu_id=t2.stu_id;
select `stu_name`, `total` from `tb_student` as `t1`
right outer join (
select `stu_id`, count(*) as `total`
from `tb_record` group by `stu_id`
) as `t2` on `t1`.`stu_id`=`t2`.`stu_id`;
```
```
上面的DML有几个地方需要加以说明:
上面的DML有几个地方需要加以说明:
1. MySQL 中支持多种类型的运算符,包括:算术运算符(`+`、`-`、`*`、`/`、`%`)、比较运算符(`=`、`<>`、`<=>`、`<`、`<=`、`>`、`>=`、`BETWEEN...AND..`.、`IN`、`IS NULL`、`IS NOT NULL`、`LIKE`、`RLIKE`、`REGEXP`)、逻辑运算符(`NOT`、`AND`、`OR`、`XOR`)和位运算符(`&`、`|`、`^`、`~`、`>>`、`<<`),我们可以在 DML 中使用这些运算符处理数据。
2. MySQL 中支持多种类型的运算符,包括:算术运算符(`+`、`-`、`*`、`/`、`%`)、比较运算符(`=`、`<>`、`<=>`、`<`、`<=`、`>`、`>=`、`BETWEEN...AND..`.、`IN`、`IS NULL`、`IS NOT NULL`、`LIKE`、`RLIKE`、`REGEXP`)、逻辑运算符(`NOT`、`AND`、`OR`、`XOR`)和位运算符(`&`、`|`、`^`、`~`、`>>`、`<<`),我们可以在 DML 中使用这些运算符处理数据。
@@ -1257,7 +1404,7 @@ select @a as 最高分, @b as 最低分, @c as 平均分;
...
@@ -1257,7 +1404,7 @@ select @a as 最高分, @b as 最低分, @c as 平均分;
删除过程。
删除过程。
```SQL
```SQL
drop procedure sp_score_by_cid;
drop procedure sp_score_stat;
```
```
在过程中,我们可以定义变量、条件,可以使用分支和循环语句,可以通过游标操作查询结果,还可以使用事件调度器,这些内容我们暂时不在此处进行介绍。虽然我们说了很多过程的好处,但是在实际开发中,如果过度的使用过程并将大量复杂的运算放到过程中,必然会导致占用数据库服务器的 CPU 资源,造成数据库服务器承受巨大的压力。为此,我们一般会将复杂的运算和处理交给应用服务器,因为很容易部署多台应用服务器来分摊这些压力。
在过程中,我们可以定义变量、条件,可以使用分支和循环语句,可以通过游标操作查询结果,还可以使用事件调度器,这些内容我们暂时不在此处进行介绍。虽然我们说了很多过程的好处,但是在实际开发中,如果过度的使用过程并将大量复杂的运算放到过程中,必然会导致占用数据库服务器的 CPU 资源,造成数据库服务器承受巨大的压力。为此,我们一般会将复杂的运算和处理交给应用服务器,因为很容易部署多台应用服务器来分摊这些压力。
...
@@ -1274,10 +1421,109 @@ MySQL 从8.0开始支持窗口函数,大多数商业数据库和一些开源
...
@@ -1274,10 +1421,109 @@ MySQL 从8.0开始支持窗口函数,大多数商业数据库和一些开源