Home > Database > Mysql Tutorial > body text

mysql中having语句与where语句的用法与区别_MySQL

WBOY
Release: 2016-06-01 13:30:39
Original
1355 people have browsed it

bitsCN.com

mysql中having语句与where语句的用法与区别

 

我们在写sql语句的时候,经常会使用where语句,很少会用到having,其实在mysql中having子句也是设定条件的语句与where有相似之处但也有区别。having子句在查询过程中慢于聚合语句(sum,min,max,avg,count).而where子句在查询过程中则快于聚合语句(sum,min,max,avg,count)。  

 

简单说来:  

 

where子句:  select sum(num) as rmb from order where id>10  //先查询出id大于10的记录才能进行聚合语句  having子句:  select reportsto as manager, count(*) as reports from employees  group by reportsto having count(*) > 4  
Copy after login

 

 

以test库为例.having条件表达示为聚合语句。肯定的说having子句查询过程慢于聚合语句。  

再换句说话说把上面的having换成where则会出错。统计分组数据时用到聚合语句。  

 

对分组数据再次判断时要用having。如果不用这些关系就不存在使用having。直接使用where就行了。  

having就是来弥补where在分组数据判断时的不足。因为where要快于聚合语句。

 

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!