分组
GROUP BY 子句将表中数据分成若干小组
- 语法格式:
-
select column,group_function(column) from table [where condition] [group by group_by_expression] [order by column]
执行顺序:from>where>group by>select
例如:
select address,SUM(stime) from t_students group by address; -以address为分组,查看分组stime总和
注意事项:
在select列表中的字段,不能是group by没有的字段,除非这些用在多行函数中。上面的例子中,只能用address字典
如果没有group by语句;
如果没有group by的子句,select列表中不允许出现字段与多行函数混用的情况;
不允许在where子句中使用多行函数;
过滤
- 对分组过滤查询的结果进行过滤,要使用having从句;
- having从句过滤分组后的结果,他只能出现在group by之后;
- where过滤行,having过滤分组,having支持所有where操作符
- 语法格式:
select column,group_function(column) from table [where condition] [group by group_by_expression] [having group_condition] [order by column]
执行顺序:from>where>group by>having>select>order by