MySql学习笔记(三) —— 聚集函数的使用

时间:2023-12-14 15:39:14

1.AVG() 求平均数

select avg(prod_price) as avg_price from products; --返回商品价格的平均值

select avg(prod_price) as avg_price from products where vend_id = 1003; --返回生产商id为1003的商品价格平均值

2.COUNT():确定表中行的数目或者符合特定条件的行的数目

select count(*) as num_cust from customers; --统计customers表中的顾客数
select count(email) as num_cust from customers; --统计具有邮件的用户数目

3.max():返回指定列中的最大值

select max(prod_price) as max_price from products; --返回价格最大值

它能够返回任意列的最大值,如果是文本数据时,会返回最后一行。

4.min():返回指定列的最小值

select min(prod_price) as max_price from products; --返回价格最小值

它能够返回任意列的最小值,如果是文本数据时,会返回最前面的行。

5.sum():返回指定列值的和

select sum(quantity) as items_ordered from orderitems where order_num = 20005;  --返回订单编号为20005所订购的商品总数
select sum(item*quantity) as total_price from orderitems where order_num = 20005;  --返回订单编号为20005所订购的商品总额