Mysql_DQL02(排序查询,聚合查询)
一、排序查询
select 字段1,字段2,...
from 表名
order by 字段名1[asc|desc],字段名2[asc|desc]...
--asc代表升序(默认),desc降序
--order by 用于子句中 可以支持单个字段,多个字段,表达式,函数,别名
去重排序(降序)
select distinct 字段 from 表名 order by 字段 desc;
二、聚合查询
1.查询总数据
select count(*) from 表名;
2.查询价格大于200的总条数
select count(id) from 表名 where 字段 > 200
3.查询分类为'c001'的商品总和 (相当于 先进行条件查询,在求和)
select sum(price) from 表名 where 字段 = c001;
4.查询最大值
select max(字段) from 表名;
5.查询最小值
select min(字段) from 表名;
6.平均价格
select avg() from 表名 where 字段 = 'c002';