1. 程式人生 > >mysql group by 分組及having 過濾分組

mysql group by 分組及having 過濾分組

基於 order 創建 tro having nbsp HERE sel strong

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 過濾分組