1.group by分组
- 按照字段分组,同一字段的数据会被分为一组
- 分组的目的是为了配合聚合函数,聚合函数会对每一组的数据分别进行统计
--select 字段1,字段2,聚合函数from 表名 group by字段1,字段2; select age,count(*)from student group by age; --select 字段1,聚合函数from 表名where 条件 group by字段1; select age,count(*)from student where sex='女' group by sex select age,count(*)from student where sex='女' group by sex order by class;
2.where、 group by 和order by顺序
select * from 表名 where 条件 group by 字段名 order by 字段名
3.where 和 having
- having子句,总是出现在group by 子句之后
- 格式:
select * from 表名 group by 字段名 having 条件
- where 时对分组前对数据进行筛选,先筛选符合条件的记录,然后在聚合统计
- having先分组聚合统计,在统计的结果中筛选
![]()
limit 显示前几行数据
2.分页