SQL分组查询语句和分页查询语句

1. 分组查询

/*
分组函数
SELECT 字段列表 FROM 表名[WHERE 分组前条件限定]GROUP BY 分组字段名 [HAVING 分组后条件过滤];
.*/
select * from stu ;

-- 1.查询男同学和女同学各自的数学平均分
select sex, avg(math) from stu group by sex;
select name, sex, avg(math) from stu group by sex;

-- 2.查询男同学和女同学各自的数学平均分,以及及各自人数
select sex, avg(math),count(*) from stu group by sex;

-- 3。查询男同学和女同学各自的数学平均分,以及及各自人数,要求:分数低于70分的不参与分组
select sex, avg(math),count(*) from stu where math > 70 group by sex; 

-- 4.查询男同学和女同学各自的数学平均分,以及及各自人数,要求:分数低于70分的不参与分组,分组之后人数大于2
select sex, avg(math),count(*) from stu where math > 70 group by sex having count(*) >2;

2. 分页查询

/*
分页查询:
SELECT 字段列表 FROM 表名 LIMIT起始索引 ,查询条目数
* 起始索引:从0开始
*/

-- 1.从0开始查询,查询3条数据
select * from stu limit 0 , 3;

-- 2.每页显示3条数据,查询第1页数据
select * from stu limit 0 , 3;

--3.每页显示3条数据,查询第2页数据
select * from stu limit 3 ,3;

-- 4.每页显示3条数据,查询第3页数据
select * from stu limit 6 ,3;

-- 起始索引 = (当前页码-1)*每页显示的条数

 

posted @ 2022-08-10 16:32  RHCHIK  阅读(485)  评论(0编辑  收藏  举报