在MySQL中,聚合查询是指通过一些特定的函数(如SUM
、AVG
、MAX
、MIN
等)根据需求来查询相关的信息。以下是一些MySQL聚合查询的例子:
SUM
函数查询某列的总和:SELECT SUM(salary) FROM employees;
AVG
函数查询某列的平均值:SELECT AVG(salary) FROM employees;
MAX
函数查询某列的最大值:SELECT MAX(salary) FROM employees;
MIN
函数查询某列的最小值:SELECT MIN(salary) FROM employees;
GROUP BY
子句对结果进行分组:SELECT department, AVG(salary) FROM employees GROUP BY department;
HAVING
子句过滤分组结果:SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 5000;
ORDER BY
子句对结果进行排序:SELECT department, AVG(salary) FROM employees GROUP BY department ORDER BY AVG(salary) DESC;
以上是一些基本的MySQL聚合查询的例子,实际使用中可能需要根据具体需求进行调整。