select stuname as 姓名, year(now())-year(stubirth) as 年龄 from tb_student where stusex=1 order by 年龄 desc;
select stuname as 姓名, year(now())-year(stubirth) as 年龄 from tb_student where stusex=1 order by 年龄 desc;
-- 聚合函数:max / min / count / sum / avg
-- 聚合函数:max / min / count / sum / avg
...
@@ -243,15 +375,10 @@
...
@@ -243,15 +375,10 @@
select max(stubirth) from tb_student;
select max(stubirth) from tb_student;
-- 查询男女学生的人数(分组和聚合函数)
-- 查询男女学生的人数(分组和聚合函数)
select count(stuid) from tb_student;
select stusex, count(*) from tb_student group by stusex;
select stusex, count(*) from tb_student group by stusex;
select stusex, min(stubirth) from tb_student group by stusex;
-- 查询课程编号为1111的课程的平均成绩(筛选和聚合函数)
-- 查询课程编号为1111的课程的平均成绩(筛选和聚合函数)
select avg(scmark) from tb_score where couid=1111;
select avg(scmark) from tb_score where couid=1111;
select min(scmark) from tb_score where couid=1111;
select count(scid) from tb_score where couid=1111;
select count(scmark) from tb_score where couid=1111;
-- 查询学号为1001的学生所有课程的平均分(筛选和聚合函数)
-- 查询学号为1001的学生所有课程的平均分(筛选和聚合函数)
select avg(scmark) from tb_score where stuid=1001;
select avg(scmark) from tb_score where stuid=1001;
...
@@ -260,8 +387,7 @@
...
@@ -260,8 +387,7 @@
select stuid as 学号, avg(scmark) as 平均分 from tb_score group by stuid;
select stuid as 学号, avg(scmark) as 平均分 from tb_score group by stuid;
-- 查询平均成绩大于等于90分的学生的学号和平均成绩
-- 查询平均成绩大于等于90分的学生的学号和平均成绩
-- 分组以前的筛选使用where子句
-- 分组以前的筛选使用where子句 / 分组以后的筛选使用having子句
-- 分组以后的筛选使用having子句
select stuid as 学号, avg(scmark) as 平均分 from tb_score group by stuid having 平均分>=90;
select stuid as 学号, avg(scmark) as 平均分 from tb_score group by stuid having 平均分>=90;
-- 查询年龄最大的学生的姓名(子查询/嵌套的查询)
-- 查询年龄最大的学生的姓名(子查询/嵌套的查询)
...
@@ -282,7 +408,7 @@
...
@@ -282,7 +408,7 @@
-- 查询学生姓名、课程名称以及成绩(连接查询)
-- 查询学生姓名、课程名称以及成绩(连接查询)
select stuname, couname, scmark from tb_student t1, tb_course t2, tb_score t3 where t1.stuid=t3.stuid and t2.couid=t3.couid and scmark is not null;
select stuname, couname, scmark from tb_student t1, tb_course t2, tb_score t3 where t1.stuid=t3.stuid and t2.couid=t3.couid and scmark is not null;
-- 内连接和分页查询
-- 查询学生姓名、课程名称以及成绩按成绩从高到低查询第11-15条记录(内连接+分页)
select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 5 offset 10;
select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 5 offset 10;
select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 10, 5;
select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 10, 5;
...
@@ -293,9 +419,8 @@
...
@@ -293,9 +419,8 @@
select stuname, avgmark from tb_student t1 inner join
select stuname, avgmark from tb_student t1 inner join
(select stuid, avg(scmark) as avgmark from tb_score group by stuid) t2 on t1.stuid=t2.stuid;
(select stuid, avg(scmark) as avgmark from tb_score group by stuid) t2 on t1.stuid=t2.stuid;
-- left outer join / right outer join / full outer join
-- 查询每个学生的姓名和选课数量(左外连接和子查询)
-- 查询每个学生的姓名和选课数量(左外连接和子查询)
select stuname, ifnull(total, 0) from tb_student t1 left outer join (select stuid, count(stuid) as total from tb_score group by stuid) t2 on t1.stuid=t2.stuid;
select stuname, ifnull(total, 0) from tb_student t1 left outer join (select stuid, count(stuid) as total from tb_score group by stuid) t2 on t1.stuid=t2.stuid;
```
```
...
@@ -306,32 +431,42 @@
...
@@ -306,32 +431,42 @@
-- 创建名为hellokitty的用户
-- 创建名为hellokitty的用户
create user 'hellokitty'@'%' identified by '123123';
create user 'hellokitty'@'%' identified by '123123';
-- 将对SRS数据库所有对象的所有操作权限授予hellokitty
-- 将对school数据库所有对象的所有操作权限授予hellokitty
grant all privileges on school.* to 'hellokitty'@'%';
grant all privileges on school.* to 'hellokitty'@'%';