首页 > 数据库技术 > 详细

mysql group by 分组及having 过滤分组

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

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 过滤分组

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!