聚合函数

215 阅读1分钟

1.常用聚合函数

image.png

2.count()--->统计函数

方法1: count(*)

方法2: count(1)

方法3: count(指定字段)

(当字段含有null时,才2有count(*)==count(1)==count(字段))

3.分组----> group by

image.png

select中的单个查询条件必须出现在group by 中作为分组条件
Select 条件1,条件2…..max()(聚合函数除外)
From employees e
group by 条件1,条件2;

4.having过滤数据--->与group by连用

image.png

当使用group by进行分组时,其中的聚合函数作为了过滤条件,必须使用having 进行(不能使用where)
Having 和 where的区别
Having:查询条件为聚合函数和一般数(可使用别名)
Where:只能是一般数(不可使用别名)

5.终结

image.png