mysql "order by" "distinct" "group by" "having"

本文用到的表结构

create table stu( 
    stu_id int auto_increment primary key, 
    name varchar(30) not null, 
    age smallint,
    cls_id int
);

  

1、order by  排序 默认升序

1)单列排序,只按照某列排序

mysql> select name, age from stu order by name;
+-----------+------+
| name      | age  |
+-----------+------+
| guojing   |   25 |
| huangrong |   23 |
| lisi      |   20 |
| wangwu    |   17 |
| xiaolongn |   14 |
| zhangsan  |   18 |
| zhangsan  |   18 |
| zhangsan  |   15 |
| zhangsan  |   17 |
| zhaoliu   |   15 |
+-----------+------+
10 rows in set (0.00 sec)

2)多列排序,当第一列值相同时,按第二列排序,以此类推多列排序

mysql> select name, age from stu where name='zhangsan' order by name,age;
+----------+------+
| name     | age  |
+----------+------+
| zhangsan |   15 |
| zhangsan |   17 |
| zhangsan |   18 |
| zhangsan |   18 |
+----------+------+
4 rows in set (0.01 sec)

3)在要进行排序的字段后边添加。降序,"order by 字段 desc"   升序  "order by 字段 asc"  没有说明的将默认升序

mysql> select name, age from stu order by name desc, age asc;
+-----------+------+
| name      | age  |
+-----------+------+
| zhaoliu   |   15 |
| zhangsan  |   15 |
| zhangsan  |   17 |
| zhangsan  |   18 |
| zhangsan  |   18 |
| xiaolongn |   14 |
| wangwu    |   17 |
| lisi      |   20 |
| huangrong |   23 |
| guojing   |   25 |
+-----------+------+
10 rows in set (0.00 sec)

 

2、distinct  将查询的结果去掉重复值

mysql> select distinct name from stu;
+-----------+
| name      |
+-----------+
| zhangsan  |
| lisi      |
| wangwu    |
| zhaoliu   |
| guojing   |
| huangrong |
| xiaolongn |
+-----------+
7 rows in set (0.00 sec)

 

3、聚合函数

聚合函数也称分组函数,总共有5个:聚合函数忽略空值

  • count    统计记录总数
  • sum     求和
  • avg      求平均值
  • max     取最大值
  • min      取最小值

1)求stu表的总记录数

mysql> select count(*) as 总记录数 from stu;
+--------------+
| 总记录数     |
+--------------+
|            7 |
+--------------+
1 row in set (0.00 sec)

2)求stu表的学生总最大值,最小值,平均值,年龄

mysql> select max(age) as 最大值, min(age) as 最小值, avg(age) as 平均值, sum(age) as 年龄总和 from stu;
+-----------+-----------+-----------+--------------+
| 最大值      | 最小值      | 平均值      | 年龄总和      |
+-----------+-----------+-----------+--------------+
|        25    |        14    |  18.8571 |          132    |
+-----------+-----------+-----------+--------------+
1 row in set (0.00 sec)

  

4、分组查询

分组查询中只能使用聚合函数与参与分组的字段

1)查询每个班的年龄最大值,最小值,平均值,总和

mysql> select cls_id, max(age), min(age), avg(age), sum(age) from stu group by cls_id;
+--------+----------+----------+----------+----------+
| cls_id | max(age) | min(age) | avg(age) | sum(age) |
+--------+----------+----------+----------+----------+
|      1 |       18 |       14 |  16.0000 |       32 |
|      2 |       25 |       17 |  21.0000 |       42 |
|      3 |       23 |       15 |  19.3333 |       58 |
+--------+----------+----------+----------+----------+
3 rows in set (0.00 sec)

 

2)查询每个班的人数

mysql> select cls_id, count(*) from stu group by cls_id;
+--------+----------+
| cls_id | count(*) |
+--------+----------+
|      1 |        2 |
|      2 |        2 |
|      3 |        3 |
+--------+----------+
3 rows in set (0.01 sec)

 

4、having

having子句用于分组后的数据筛选,不用where的原因是where不能和聚合函数一起使用,这里涉及到sql的执行顺序的问题

1)查找平均年龄大于18的班级

mysql> select cls_id, avg(age) from stu group by cls_id having avg(age) > 18;
+--------+----------+
| cls_id | avg(age) |
+--------+----------+
|      2 |  21.0000 |
|      3 |  19.3333 |
+--------+----------+
2 rows in set (0.00 sec)

 

posted @ 2018-10-22 10:23  nonzero  阅读(290)  评论(0编辑  收藏  举报