[TOC=1,5]
>[success] # 聚集函数
>在应用中,经常需要汇总数据而不用把它们实际检索处理,MySQL 提供了专 门的聚集操作函数,如下图所示:
![](https://box.kancloud.cn/55e2b05df8465919539193420abc55f7_798x343.png)
<br>
>[danger] ##### 返回平均值 - AVG
`SELECT AVG(prod_price) AS avg_price FROM products;`
![](https://box.kancloud.cn/551efefb4ce90e8b1ffbfe3575dd9fa8_131x46.png)
**注:**
配合where 条件计算
`SELECT AVG(prod_price) AS avg_price FROM products WHERE vend_id=1003;`
![](https://box.kancloud.cn/a558505057f49c9506fd32c02415fcae_127x50.png)
* * * * *
<br>
>[danger] ##### 返回列的行数 - count
`SELECT COUNT(*) AS num_cust FROM products;`
![](https://box.kancloud.cn/58bdb4d25cb88f53825366c24d07e497_119x40.png)
* * * * *
<br>
>[danger] ##### 返回最大值 - max
`SELECT MAX(prod_price) AS max_price FROM products;`
![](https://box.kancloud.cn/cd1f9ebe2a17e46003bc4f4d2c1132cb_127x39.png)
**注:用limit**
`SELECT prod_price AS max_price FROM products ORDER BY prod_price DESC LIMIT 1;`
* * * * *
<br>
>[danger] ##### 返回最小值 - min
`SELECT min(prod_price) AS max_price FROM products;`
* * * * *
<br>
>[danger] ##### 返回和 - sum
`SELECT SUM(prod_price) AS max_price FROM products;;`
![](https://box.kancloud.cn/34261a2449309f6aacb0ab645dafb008_138x39.png)
* * * * *
<br>
>[danger] ##### 将几个函数组合使用
![](https://box.kancloud.cn/3af257c3dcf341801f0f0f9de85bf400_822x151.png)