聚合函数:
select avg(salary)//平均值
from wsb;
select sum(salary)//总和
from wsb;
select max(salary)//最大
from wsb;
select min(salary)// 最小
from wsb;
select count(*)// 统计多少数据 配合where salary>500;
from wsb;
分组:分别计算 group by
select sex,count(*)
from wsb2 group by sex; group by后面不能有where只能用having
where后面不能跟聚合函数having可以
eg1:
select a.Name,SUM(b.Grade)
from students a ,Score b
GROUP BY a.Id,b.Stu_id, a.Name
HAVING SUM(b.Grade)>100;
eg:
select a.Name,SUM(b.Grade)
from students a ,Score b
GROUP BY a.Id,b.Stu_id
HAVING a.Id=b.Stu_id; having如果后面没有跟聚合函数 那么对应字段必须在group by中有相应字段
join:
Left join:左连接, 连接两张表,以左边表的数据匹配右边表中的数据,如果左边
表中的数据在右边表中没有,会显示左边表中的数据。
Right join:右连接,连接两张表,以右边表的数据匹配左边表中的数据,如果左边
表中的数据在左边边表中没有,会显示右边表中的数据。
Inner join:内连接,连接两张表,匹配两张表中的数据,和前面两个不同的是只会
显示匹配的数据。
select a.name 学生姓名,b.score 学生成绩 from students a left join score b on
a.id=b.student_id;
select a.name 学生姓名,b.score 学生成绩 from students a right join score b on
a.id=b.student_id;
select a.name 学生姓名,b.score 学生成绩 from students a INNER join score b on
a.id=b.student_id;
select a.name 学生姓名,b.score 学生成绩 from students a,score b where
a.id=b.student_id;
表中的数据在右边表中没有,会显示左边表中的数据。
Right join:右连接,连接两张表,以右边表的数据匹配左边表中的数据,如果左边
表中的数据在左边边表中没有,会显示右边表中的数据。
Inner join:内连接,连接两张表,匹配两张表中的数据,和前面两个不同的是只会
显示匹配的数据。
select a.name 学生姓名,b.score 学生成绩 from students a left join score b on
a.id=b.student_id;
select a.name 学生姓名,b.score 学生成绩 from students a right join score b on
a.id=b.student_id;
select a.name 学生姓名,b.score 学生成绩 from students a INNER join score b on
a.id=b.student_id;
select a.name 学生姓名,b.score 学生成绩 from students a,score b where
a.id=b.student_id;