MySQL数据库:聚合函数的使用

聚合函数

max() 最大值
min() 最小值
avg() 平均值
sum() 求和
count() 符合条件数据的数目

聚合函数不能嵌套使用

# 在统计时字段内没有满足条件的数值只有count返回数值0或者其他,而其余四个聚合函数返回null;
# 对于聚合函数的参数,很多时候使用字段名来表示,那么这个时候,该字段内的null值不参与统计
count(*)
显示表中符合条件的信息数目,不考虑某字段出现null值
select count(cID),avg(elScore) from elogs;
select count(elNo),avg(elScore) from elogs;
select count(1),avg(elScore) from elogs;
select count(*),avg(elScore) from elogs;
转换编码
# 中文排序想要通过拼英排序的话需要转换编码
convert (tName using gbk)
去重复值
# distinct 后面如果有多个字段,则是针对这些字段值组合后的去重
select distinct sID from Elogs;
# 先去重,再统计
select count(distinct cID) from Elogs;

= 只有确定了一个值才能用 多条值用in

posted @ 2019-11-21 23:19  软二的小忠晏  阅读(974)  评论(0编辑  收藏  举报