Packet statement
- Field of the packet according to represent the same data in this field will be placed in a group
- After grouping, grouped according to the column will appear in the result, the other columns will not appear in the result
- Statistical data can be grouped, do aggregation operation
- grammar:
select 列1,列2,聚合... from 表名 group by 列1,列2...
One case: the number of queries of various sex
select sex,count(*) from students group by sex
Example 2: The number of queries of all ages
select age,count(*) from students group by age
Exercise
查询各个班级学生的平均年龄、最大年龄、最小年龄
After filtering data packets
- grammar:
select 列1,列2,聚合... from 表名
group by 列1,列2,列3...
having 列1,...聚合...
- Where the same conditions with the operator having the back
Example 1: Query the total number of boys
方案一
select count(*) from students where sex='男'
-----------------------------------
方案二:
select sex,count(*) from students group by sex having sex='男'
Exercise
查询1班除外其他班级学生的平均年龄、最大年龄、最小年龄
Contrast where and having
- where behind is specified from table data filtering, filtering the raw data belonging to
- Is the group by having the results of screening