第十课 分组数据
10.1 创建分组
select vend_id,count(1),avg(prod_price) from products group by vend_id
select vend_id,prod_name,count(1),avg(prod_price) from products group by vend_id,prod_name
可以选择group by 多个列
10.2 过滤分组
WHERE 不能完成任务,因为 WHERE 过滤指定的是行而不是分组。事实上, WHERE 没有分组的概念。
select vend_id,count(1),avg(prod_price) from products group by vend_id having count(1) >2
where 和having一起用,where用于过滤出来行,之后having过滤分组
select count(1) from products where prod_price > 4 group by vend_id HAVING count(1) > 2;
10.3 分组和排序
可以在having后面再加入order by
select vend_id,prod_name,count(1),avg(prod_price) from products group by vend_id,prod_name order by avg(prod_price) desc
10.4 select 字句顺序
注意这几个字句的顺序!!!