使用聚合函数查询
加入存在如下数据表(表名grade):
1、COUNT()函数
COUNT()用来统计记录的总条数,一般与GROUP BY关键字一起使用
SELECT COUNT(*) FROM `grade`;
SELECT COUNT(*) AS total FROM `grade`;
SELECT stuName,COUNT(*) AS total FROM grade GROUP BY stuName;
2、SUM()函数
SUM()函数是求和函数 与GROUP BY关键字一起使用
SELECT stuName,SUM(grade) FROM grade WHERE stuName='小王';
SELECT stuName,SUM(grade) FROM grade GROUP BY stuName;
3、AVG()函数
AVG()函数是求平均值的函数与GROUP BY关键字一起使用
SELECT stuName,AVG(grade) FROM grade WHERE stuName='小王';
SELECT stuName,AVG(grade) FROM grade GROUP BY stuName;
3、MAX()函数
MAX()函数是求最大值的函数与GROPE BY关键字一起使用
SELECT stuName,course,MAX(grade) FROM grade WHERE stuName='小王';
SELECT stuName,course,MAX(grade) FROM grade GROUP BY stuName;
4、MIN()函数
MIN()函数是求最大值的函数与GROPE BY关键字一起使用
SELECT stuName,course,MIN(grade) FROM grade WHERE stuName='小王';
SELECT stuName,course,MIN(grade) FROM grade GROUP BY stuName;
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/4469.html