# 分组操作

group by 指的是：将所有记录按照某个相同字段进行归类

• group by是分组的关键字

• group by 必须和聚合函数一起使用

--以性别为例， 进行分组， 统计一下男生和女生的人数是多少个
select count(id), gender from students group by gender;
+-----------+--------+
| count(id) | gender |
+-----------+--------+
|         2 | 男     |
|         3 | 女     |
|         1 | 中性   |
+-----------+--------+

--对班级进行分组，统计出每个班级年龄最大的那个人

select cls_id,max(age),group_concat(name) from students group by cls_id;
+--------+----------+----------------------+
| cls_id | max(age) | group_concat(name)   |
+--------+----------+----------------------+
|      1 |       30 | 张飞,关羽,小鬼       |
|      2 |       18 | 小乔,孙尚香          |
|      3 |       20 | 甄姬                 |
+--------+----------+----------------------+

# 常用的聚合函数

#求最大年龄
mysql> select  MAX(age)   from  student_detail;

#求最小年龄
mysql> select  MIN(age) from student_detail;

#求和
mysql> select  SUM(age)  from  student_detail;

#求平均数
mysql> select  AVG(age)   from  student_detail;

#四舍五入
mysql> select  ROUND(AVG(age))   from   student_details;

#统计
mysql> select count(s_id)   from  student;

# having 二次删选

-- 查询平均年龄超过20岁的性别，以及姓名
select gender,group_concat(name) from students group by gender having avg(age)>20;
+--------+--------------------+
| gender | group_concat(name) |
+--------+--------------------+
| 男     | 张飞,关羽          |
+--------+--------------------+

# 排序

order by 字段名 asc(升序) desc(降序)

-- 查询年龄在18到30岁之间的男性，按照年龄从小到大排序
select * from students where age between 18 and 30 and gender='男' order by age asc;
+----+--------+------+--------+--------+--------+
| id | name   | age  | height | gender | cls_id |
+----+--------+------+--------+--------+--------+
|  5 | 关羽   |   28 | 190.00 | 男     |      1 |
|  4 | 张飞   |   30 | 190.00 | 男     |      1 |
+----+--------+------+--------+--------+--------+

select * from students order by age desc,id asc;
+----+-----------+------+--------+--------+--------+
| id | name      | age  | height | gender | cls_id |
+----+-----------+------+--------+--------+--------+
|  4 | 张飞      |   30 | 190.00 | 男     |      1 |
|  5 | 关羽      |   28 | 190.00 | 男     |      1 |
|  3 | 甄姬      |   20 | 170.00 | 女     |      3 |
|  1 | 小乔      |   18 | 180.00 | 女     |      2 |
|  2 | 孙尚香    |   18 | 180.00 | 女     |      2 |
|  6 | 小鬼      |   16 | 178.00 | 中性   |      1 |
+----+-----------+------+--------+--------+--------+

# 分页查询

offset: 行数据索引

size: 取多少条数据

select * from students limit 3,5;
+----+--------+------+--------+--------+--------+
| id | name   | age  | height | gender | cls_id |
+----+--------+------+--------+--------+--------+
|  4 | 张飞   |   30 | 190.00 | 男     |      1 |
|  5 | 关羽   |   28 | 190.00 | 男     |      1 |
|  6 | 小鬼   |   16 | 178.00 | 中性   |      1 |
+----+--------+------+--------+--------+--------+

--limit  可以把offset省略，默认从第一行取
select * from students limit 5;
+----+-----------+------+--------+--------+--------+
| id | name      | age  | height | gender | cls_id |
+----+-----------+------+--------+--------+--------+
|  1 | 小乔      |   18 | 180.00 | 女     |      2 |
|  2 | 孙尚香    |   18 | 180.00 | 女     |      2 |
|  3 | 甄姬      |   20 | 170.00 | 女     |      3 |
|  4 | 张飞      |   30 | 190.00 | 男     |      1 |
|  5 | 关羽      |   28 | 190.00 | 男     |      1 |
+----+-----------+------+--------+--------+--------+

select * from 表名 where 条件 group by 条件 having 条件 order by 条件 limit 条件

posted @ 2019-10-30 22:34  SetCreed  阅读(...)  评论(... 编辑 收藏