聚集函数(Aggregate function): COUNT(), AVG(), SUM(), MAX(), MIN()
1. COUNT(*): 计算数据表中的总行数,包括NULL值行。
2. COUNT(column): 计算某一列的行数,不包括NULL值行。
AVG(): 用来返回特定数值列的平均值,注意列的类型必须是数值型!忽略值为NULL的行。
SUM(): 用来返回指定列值的总和。忽略NULL值的行。
计算不同价格的平均值, DISTINCT
select AVG(DISTINCT prod_price) as avg_price from Products where vend_id='DLL01';
**********分割线**************
Group By 子句必须出现在where子句之后, order by 子句之前。
where 与 having 的区别
where 用于过滤行, having过滤分组
select id, name from students where sex='male';
从学生表里选择性别为男的行,并且返回这些行里的id,和name。
where 在数据分组前进行过滤,having在数据分组后进行过滤。where 排除的行不包括在分组中。
having 只在group by 子句之后使用。
select * from products where num in ( select num from products group by num having count(num)>1);
select vend_id, count(*) from products where prod_price>=4 group by vend_id having count(*)>=2;