DQL分组查询
where 和 having的区别:
* 执行时机不一样:where 是分组之前进行限定,不满足where条件,则不参与分组,而having是分组之后对结果进行过滤
* 可判断时机不一样:where不能对聚合函数进行判断,having可以
-- 分组查询GROUP BY -- 注意:分组之后,查询分组字段和聚合函数,查询其他字段毫无意义 -- 执行排序 :where > 聚合函数 > HAVING /* 分组函数: select 字段列表 from 表名 [where 分组前条件限定] group by 分组字段名字 [HAVING 分组后的条件过滤]; */ -- 1,查询不同地址的电话平均分 select AgentAddress , AVG(Phone) from agent GROUP BY AgentAddress; -- 2,查询不同地址的电话平均分,以及各自人数 select AgentAddress , AVG(Phone) ,count(*) from agent GROUP BY AgentAddress; -- 3,查询不同地址的电话平均分,以及各自人数,低于1000的不参与分组 select AgentAddress , AVG(Phone) ,count(*) from agent where Phone > 1000 GROUP BY AgentAddress; -- 4,查询不同地址的电话平均分,以及各自人数,低于1000的不参与分组,分组之后人数大于两个 select AgentAddress , AVG(Phone) ,count(*) from agent where Phone > 1000 GROUP BY AgentAddress having count(*)>2;