101 . python高级------MySQl数据库的条件查询(3)

101 . python高级------MySQl数据库的条件查询(3)

python修炼第二十四天

2019年 4月 23日 晴

4.分组

group by
– 按照性别分组,查询所有的性别
select 分组字段 from 表名 group by 分组字段 having 分组的条件;

select gender from students group by gender;

– 计算每种性别中的人数

select gender,count(*) from students group by gender;

group_concat(...)
– 查询同种性别中的姓名
– 返回一组的姓名

select gender,group_concat(name) from students group by gender;

– 查询每组性别的平均年龄

select gender ,avg(age) from students group by gender;

– having(注意having和group by 连用 having后通常也要跟 聚合函数)
– 查询平均年龄超过30岁的性别,以及姓名

select gender ,avg(age), group_concat(name) from students 
group by gender having avg(age) > 30;

– 查询每种性别中的人数多于2个的信息

select gender,count(*) from students group by gender
 having count(*) > 2;

with rollup 汇总的作用(了解)

--select gender,count(*) from students group by gender with rollup;


select gender,count(*) from students group by gender
  with rollup having count(*) > 2 ;

5. 分页

limit start, count
– limit 放在最后面(注意)

起始人位置  = (页面-1)*每一页的个数

– 限制查询出来的数据个数
– 查询前5个数据

select * from students limit 0,5;

– 每页显示2个,第1个页面

select * from students limit 0,2;

– 每页显示2个,第2个页面

select * from students limit 2,2;

– 每页显示2个,第3个页面

select * from students limit 4,2;

– 每页显示2个,第4个页面

select * from students limit 6,2;

– 每页显示2个,显示第6页的信息, 按照年龄从小到大排序

select * from students order by age asc limit 10,2;

猜你喜欢

转载自blog.csdn.net/qq_40455733/article/details/89503613
101