mysql group by 分组及having 过滤分组

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子句中基于这些值过滤掉的分组)

猜你喜欢

转载自www.cnblogs.com/yuiqng/p/9175129.html