1 #二、分组函数 2 /* 3 功能:用作统计使用,又称为聚合函数或统计函数或组函数 4 5 分类: 6 sum求和、avg平均值、 max最大值、min最小值、count计算个数 7 8 特点: 9 1、sum、avg一般用于处理数值型 10 max、min、count可以处理任何类型 11 2、以上分组函数都忽略null值 12 3、可以和distinct搭配使用,实现去重的运算 13 4、count函数的单独介绍 14 一般使用count(*)用作统计行数 15 5、和分组函数一同查询的字段要求是group by后的字段 16 */ 17 18 #1.简单的使用 19 SELECT SUM(salary) FROM employees; 20 SELECT AVG(salary) FROM employees; 21 SELECT MIN(salary) FROM employees; 22 SELECT MAX(salary) FROM employees; 23 SELECT COUNT(salary) FROM employees; 24 25 SELECT SUM(salary) 和,AVG(salary) 平均,MAX(salary) 最高, 26 MIN(salary) 最低, COUNT(salary) 个数 27 FROM employees; 28 29 SELECT SUM(salary) 和,ROUND(AVG(salary)) 平均,MAX(salary) 最高, 30 MIN(salary) 最低, COUNT(salary) 个数 31 FROM employees; 32 33 #2.参数支持哪些类型 34 SELECT MAX(last_name),MIN(last_name) FROM employees; 35 36 SELECT MAX(hiredate),MIN(hiredate) FROM employees; 37 38 SELECT COUNT(commission_pct) FROM employees; 39 SELECT COUNT(last_name) FROM employees; 40 41 #3.是否忽略null 42 SELECT SUM(commission_pct),AVG(commission_pct), 43 SUM(commission_pct)/35,SUM(commission_pct)/107 44 FROM employees; 45 46 SELECT MAX(commission_pct),MIN(commission_pct) FROM employees; 47 48 SELECT COUNT(commission_pct) FROM employees; 49 50 #4.和distinct搭配 51 SELECT SUM(DISTINCT salary),SUM(salary) FROM employees; 52 53 SELECT COUNT(DISTINCT salary),COUNT(salary) FROM employees; 54 55 #5.count函数的详细介绍 56 SELECT COUNT(salary) FROM employees; 57 58 SELECT COUNT(*) FROM employees; -- 107 59 60 SELECT COUNT(1) FROM employees; -- 107 61 #效率: 62 /* 63 MYISAM存储引擎下,count(*)的效率最高 64 INNODB存储引擎下,count(*)和count(1)的效率差不多,比count(字段)要高一些 65 */ 66 67 #6.和分组函数一同查询的字段有限制 68 -- select avg(salary),employee_id from employees; 69 70