DQL聚合函数/分组查询/分页查询

-------------------------聚合函数-------------------------------------------------------

查班里一共多少学生:

  select count(id) from stu;      -- count()统计的列名不能为null

  select count(*) from stu;      -- 推荐写*

查班里数学最高/低分/平均分:

  select min(math) from stu;    -- 聚合函数里null不参与高低

  select max(math) from stu;  

  select avg(math) from stu;  

 

----------------------------------分组查询-----------------------------------------------------

分男女查数学平均分:

  select sex, avg(math) from stu group by sex;  -- select之后的字段只能是分的组或者聚合函数,其他无意义

分男女查数学平均分以及各自人数:

  select sex, avg(math), count(*) from stu group by sex;

分男女查数学平均分以及各自人数,分数低于70的不参与:

  select sex, avg(math), count(*) from stu where math>70 group by sex; -- 分组之前

分男女查数学平均分以及各自人数,分数低于70的不参与,分组后人数>2:

select sex, avg(math), count(*) from stu where math>70 group by sex having count(*)>2; -- 分组之后  

 

where里不能判断聚合函数,having可以

执行顺序:where> 聚合函数>having

 

-------------------------分页查询--------------------------------------

从0开始,查询3条数据      --起始索引=(当前页码-1)*每页显示条数

select * from stu limit 0,3;    --(1-1)*3=0

每页查3条,查第1页数据

select * from stu limit 0,3;

每页查3条,查第2页数据    

select * from stu limit 3,3;    --(2-1)*3=3

每页查3条,查第3页数据

select * from stu limit 6,3;    --(3-1)*3=6

 

===========分页方言========================

mysql用limit

oracle用rownumber

sql sever 用top

 

posted @   大灰狼21  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示