作用:把行按照字段分组
Select column, .. from table_name group by column1, column2; 使用场景:常见于统计场合,计算平均分,统计数据量等 查询每个部门的平均工资 select dept, avg(salary) from emp group by dept; 显示每个部门中的每种岗位的平均工资和最低工资 select dept, avg(salary), min(salary) from emp group by dept; select dept,job, avg(salary), min(salary) from emp group by dept,job; select dept,job, name,avg(salary), min(salary) from emp group by dept,job,name;
使用GROUP BY 子句对查询的结果进行分组
select column, .. from table_name group by column having ...;
having用于对分组结果继续过滤
where 和having 的区别:
where用于在原始数据中进行查询
having用于在结果集中进行过滤
当一个语句中既有where 又有having时,先执行where 后执行having
显示平均工资低于6000 的部门名称和他的平均工资
select dept, avg(salary) from emp group by dept having avg(salary) <7000;
分页查询
select *(北联网教程,专业提供视频软件下载)
……