数据库(5)聚合、分组、排序、分页

来源:互联网 发布:中国保险保险网络大学 编辑:程序博客网 时间:2024/06/06 03:14

聚合

count(*) 统计总行数

select count(*) from table where id_delete=0;

max(列) 求该列的最大值

select max(id) from table where gender=0;

min(列 )求该列的最小值

select min(id) from table where gender=1;

sum(列) 求列的和

同上

avg(列) 求列的平均值

 同上

分组

关键字: group by

eg: select count(*) from table broup by gender;

分组后的数据筛选:

关键字:having

eg: select gender,count(*) as num from table broup by gender having num > 2;

where VS having

1) where是对from后面指定的表进行数据筛选,属于对原始数据的筛选;
2)having是对group by分组后的结果进行筛选。

排序

关键字:order by

eg:select * from table order by row1 asc|desc, row2 asc|desc...;默认升序asc 为升序desc 为降序

分页

应用场景:数据量过大,在一页中查看数据较麻烦

语法:select * from table limit start, count;

从start开始,获取count条数据

start索引从0开始

原创粉丝点击