單個聚集函數
avg():
- 行數計數,計算特定列值之和,求得該列的平均值
- 必須給出列名作爲函數的參數
- 忽略列值爲null的行
selete avg(p_price) as avg_price
from products;
進一步:1003供應商的產品的平均價格
selete avg(p_price) as avg_price
from products
where v_id=1003;
count():
兩種使用方式:
- count(*)對錶中的行進行統計,不管表中的列值是否爲空;
- count(col)對特定的列的非空值行進行統計,忽略null的行。
selete count(*)as num
from tb;
max():
- 返回列中的最大值
- 忽略列值爲null的行
對非數值使用max():MySQL允許它返回任一列中的最大值,如文本、日期。
sum():
返回指定列值的和
聚集不同值:distinct
例:不同價格的平均值selete avg(distinct p_price)as avg_price
from products
where v_id=1003;
組合聚集函數:
selete count(*) as num,min(p_price) as max_p_price,
max(p_price) as min_p_price,
avg(p_price) as avg_p_price
from products;