一、知识
有时候,我们需要对某列数据做简单处理。(求和,求平均、取最大、取最小等)就需要用到聚集函数。聚集函数有如下5个。
AVG()//取平均
COUNT()//取这一列的行数
MAX()//取这一列的最大值
MIN()//取这一列的最小值
SUM()//取这一列之和
试举一例:
select avg(prod_price)
as avg_price
from tyqsl2.products
再举一例:
SELECT count(*) as numbers_of_items,
avg(prod_price) as avg_price,
max(prod_price) as max_price,
min(prod_price) as min_price
FROM tyqsl2.products;
二、课后习题
SELECT sum(quantity)
FROM tyqsl2.orderitems;
SELECT sum(quantity)
FROM tyqsl2.orderitems
where prod_id = 'BR01'
select max(prod_price)
as special_price
from tyqsl2.products
where prod_price <= 10