统计函数的使用

统计函数
1.count的使用
-- 演示 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
​

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值