WHERE过滤行,而HAVING过滤分组。HAVING支持所有WHERE操作符,HAVING子句中能使用聚集函数。WHERE在数据分组前进行过滤。HAVING在数据分组后进行过滤
where 可以单独用. having 必须跟group by一起用.
除聚集计算语句外,SELECT语句中的每个列都必须在GROUP BY子句中给出
当在查询中没有使用GROUP BY子句时,数据库就把数据表 中的所有行为作为一个组来处理


1
2
3
select sno,count(pno) from sc where grade>=90 group by sno having count(pno)>=2
select customer from orders group by customer having sum(orderPrice)<2000
select customer,sum(orderPrice) from orders where customer in ('Bush','Adams') group by customer having sum(orderPrice)>1500
1
2
3
4
5
6
select from_unixtime(ctime,'%Y-%m-%d') as dorderby,count(ctime)  as count
from order
where ctime > 1511712000 and status not in (3,9,18,20,30) and serv_type=0
group by from_unixtime(ctime,'%Y-%m-%d')
having count(ctime)>150
order by dorderby desc

group by可以可以使用函数; 并且可以将这个参数应用到select中.
order by 使用的列的名字是对结果集和默认中选出来的.


聚集函数: avg;count;max;min;sum