欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mysql学习随笔------常用聚合函数

程序员文章站 2022-05-30 10:22:50
...
	一般来说,比较常用的聚合函数就是count()总数  sum()和  avg()平均值  max()最大值  min()最小值,		
	聚合函数一般都是与group by 一起使用
		create table t_grade(
			id int(5) primary key auto_increment,
			stuName varchar(20) comment '姓名',
			course varchar(20) comment '科目',
			score int comment '分数'
		);

###count()函数
	select count(*) as total from t_grade;
	select stuName,count(*) as total from t_grade group by stuName;

###sum函数
	select stuName,sum(score) as '总分' from t_grade where stuName='张三';
	select stuName,SUM(score) as '总分' from t_grade group by stuName;
	
###avg()函数
	select stuName,AVG(score) as '平均分' from t_grade where stuName="张三";
	select stuName,Avg(score) as '平均分' from t_grade GROUP BY stuName;
	
###max函数
	select stuName,course ,max(score) as '最高分' from t_grade where stuName="张三";
	select stuName,MAX(score) as '最高分' from t_grade group by stuName;
	
####min函数
	select stuName,course ,min(score) as '最低分' from t_grade where stuName="张三";
	select stuName,min(score) as '最低分' from t_grade group by stuName;