HAVING和WHERE的区别

1.类型:

“Where”是一个约束声明,在查询数据库的结果返回之前对数据库中的查询条件进行约束,即在结果返回之前起作用,且where后面不能使用“聚合函数”;

“Having”是一个过滤声明,所谓过滤是在查询数据库的结果返回之后进行过滤,即在结果返回之后起作用,并且having后面可以使用“聚合函数”。

2.使用的角度:

where后面之所以不能使用聚合函数是因为where的执行顺序在聚合函数之前,

如下面这个sql语句:select  sum(score) from student  group by student.sex where sum(student.age)>100;

having既然是对查出来的结果进行过滤,那么就不能对没有查出来的值使用having,

如下面这个sql语句:  select  student.id,student.name from student having student.score >90;

原文地址:https://www.cnblogs.com/konglxblog/p/10136505.html