-- 演示 mysql 的统计函数的使用
-- 统计一个班级共有多少学生?
select count(*) from student
-- 统计数学成绩大于 90 的学生有多少个?
select count(*) from student where math > 90
-- 统计总分大于 250 的人数有多少?
select count(*) from student where (chinese + math + english) > 250
-- count(*) 和 count(列) 的区别
create table t1(
`name` varchar(32));
insert into t1 values('张三');
insert into t1 values('李四');
insert into t1 values('王五');
insert into t1 values('');
insert into t1 values(null)
select count(*) from t1 ; -- 5个
select count(`name`) from t1; -- 4个
-- 解释 :count(*) 返回满足条件的记录的行数
-- count(列): 统计满足条件的某列有多少个,但是会排除为 null
2.sum的使用
-- 演示 sum 函数的使用
-- 统计一个班级数学总成绩?
select sum(math) as total_math_score from student
-- 统计一个班级语文、英语、数学各科的总成绩
select sum(chinese),sum(math),sum(english) from student
-- 统计一个班级语文、英语、数学的成绩总和
select sum(math + english + chinese) as total_score from student
-- 统计一个班级语文成绩平均分
select sum(chinese)/count(*) from student
3.avg(平均分)的使用
-- 演示 avg 的使用
-- 练习:
-- 求一个班级数学平均分?
select avg(math) as math_average_score from student
-- 求一个班级总分平均分
select avg(math + english + chinese) as total_average_score from student
4.max和min
-- 演示 max 和 min 的使用
-- 求班级最高分和最低分(数值范围在统计中特别有用)
select max(chinese+ math+ english) from student;
select min(chinese+ math+ english) from student
-- 求出班级数学最高分和最低分
select max(math) from student;
select min(math) from student