码迷,mamicode.com
首页 > 数据库 > 详细

mysql group by 分组及having 过滤分组

时间:2018-06-12 22:27:12      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:product   替换   SQ   nbsp   rom   不能   数据   strong   str   

1,group by 创建分组

 在mysql中 group by 句子要注意,必须where 之后,order by 之前

select order_code ,count(*) as product from order  group by order_code;

2,having 分组过滤

where子句都可以用having代替,区别在于where过滤行,having过滤分组;having支持所有的where操作符

但where不能用于替换having 分组滤

select order_code ,count(*) as product from order  group by order_code having count(*)>2;

having和where的区别:

where在数据分组前进行过滤,having在数据分组后进行过滤;where排除的行不包括在分组中(这可能会改变计算值,从而影响having子句中基于这些值过滤掉的分组)

 

mysql group by 分组及having 过滤分组

标签:product   替换   SQ   nbsp   rom   不能   数据   strong   str   

原文地址:https://www.cnblogs.com/yuiqng/p/9175129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!