where 和 having 怎么区别

来源:互联网 发布:nginx绑定域名非80端口 编辑:程序博客网 时间:2024/04/27 23:30

select productid,sum(quantity) as total_quantity
from [order details]
where productid<=10
group by productid
having sum(quantity)>500
order by total_quantity
最简单的语句了
但是我不明白
where语句和having语句怎么理解
我感觉having就是 where的意思
我不会用错
就是不知道怎么理解
谢谢

 

where 是先筛选再group
having 是先group再筛选
没有group是不能having的

区别在于:在他们后面的条件里如果有count之类的聚合函数的时候只能使用having而不能使用where。