The group function is for a non-null value for the specified field. NOTE: Group functions cannot appear in the WHERE clause!!!
AVG ()average (only for numeric type)Max ()Maximum value (unrestricted type)Min ()Minimum value (unrestricted type)Count ()count (number of records, number of rows)StdDev ()variance (only for numeric type)SUM ()sum (only for numeric type) Prerequisites: Person Table
id namedept_idsalarymanager_id
0001wangda101 8500[NULL]
0002wanger1033 000 0009
0003wangsan102 4000 0006
0004wangsi104 2000 0005
0005wangwu104 6000 0001
0006wangliu102 7000 0001
0007wangqi105 5000 0008
0008wangba105 5500 0001
0009wangjiu103 6000 0001
0010wangshi104 900 0005
0011wangsy103 5000 0009
0012wangse[NULL]3000[NULL]
Group by sort (can have one or more fields) function: Check the average salary of each department according to department
SELECT
dept_id,
avg(salary)
FROM
person
GROUP BY
dept_id;
Results:
dept_idavg(salary)
[NULL]3000.000000
101 8500.000000
102 5500.000000
103 4666.666667
104 2966.666667
105 5250.000000
HavingWhen you include a group function in a query condition, it is used instead of where (note: In MySQL, the having can only be placed behind GROUP by!) )function: The Department name (Dept table), Department average salary (person table, Group function) is queried for department average salary above 5000
SELECT
p.dept_id,
dept_name,
avg(salary)
FROM
person p,dept d
where p.dept_id = d.dept_id
GROUP BY
p.dept_id
HAVING
avg(salary)>5000;
Results:
dept_iddept_nameavg(salary)
101 zongwu 8500.000000
102 zhenggong5500.000000
105 renshi 5250.000000
MySQL grouping function