
目标:
1、聚合函数
2、多表链接查询
知识点一:聚合函数
--max() min()
select MAX(cj) as 最大值 from score
select MIN(cj) as 最小值 from score
--sum() avg():
select SUM(cj) as 总成绩 from score
select AVG(cj) as 平均成绩 from score
--count():统计表中的行数
select COUNT(*) as 总行数 from score
--一个语句里可以有多少聚合函数
select MAX(cj) as 最大值,MIN(cj) as 最小值,SUM(cj) as 总成绩,AVG(cj) as 平均成绩,COUNT(*) as 总行数 from score
--注:聚合函数不能跟列名一起用,如果要一起用,必须使用分组查询
select MAX(cj) as 最大值,sno from score
知识点二:group by分组查询
1. where不能和聚合函数一起用:select sno,COUNT(*) from score where cj>60
2. where不能和聚合函数一起用必须分组:
select sno,COUNT(*) from score where cj>60 group by sno
3. Having用于分组后的条件筛选,并且可以跟聚和函数,而where不可以
如:查询各科总成绩少于330的学员
select sno,SUM(cj) from score group by sno