数据表的分组查询


分页查询

使用SELECT查询时,如果结果集数据量很大,比如几万行数据,放在一个页面显示的话数据量太大,不如分页显示。

分页实际上就是从结果集中“截取”出第M~N条记录。这个查询可以通过LIMIT OFFSET 子句实现。

比如,查询student数据表从第4条记录开始,查询一页3条数据。注意OFFSET从0开始。

                    SELECT * FROM student LIMIT 3 OFFSET 3;
                
                    mysql>  SELECT * FROM student LIMIT 3 OFFSET 3;
                    +----+------+---------+--------+--------+
                    | id | age  | classId | gender | name   |
                    +----+------+---------+--------+--------+
                    |  4 |   10 |       2 | F      | 小娟   |
                    |  5 |    9 |       3 | F      | 小菊   |
                    |  6 |    8 |       2 | M      | 晓东   |
                    +----+------+---------+--------+--------+
                    3 rows in set (0.01 sec)
                

也可以将查询子句写为LIMIT ,

                    mysql> SELECT * FROM student LIMIT 3,3;
                    +----+------+---------+--------+--------+
                    | id | age  | classId | gender | name   |
                    +----+------+---------+--------+--------+
                    |  4 |   10 |       2 | F      | 小娟 |
                    |  5 |    9 |       3 | F      | 小菊 |
                    |  6 |    8 |       2 | M      | 晓东 |
                    +----+------+---------+--------+--------+
                    3 rows in set (0.00 sec)
                

聚合查询

对于统计总数、平均数这类计算,SQL提供了专门的聚合函数,使用聚合函数进行查询,就是聚合查询,它可以快速获得结果。

SUM
计算某一列的合计值,该列必须为数值类型
AVG
计算某一列的平均值,该列必须为数值类型
MAX
计算某一列的最大值
MIN
计算某一列的最小值
                    mysql> SELECT COUNT(*),AVG(age),SUM(age),MAX(age),MIN(age) FROM student;
                    +----------+----------+----------+----------+----------+
                    | COUNT(*) | AVG(age) | SUM(age) | MAX(age) | MIN(age) |
                    +----------+----------+----------+----------+----------+
                    |        8 |   8.7500 |       70 |       10 |        8 |
                    +----------+----------+----------+----------+----------+
                    1 row in set (0.00 sec)
                

上述命令所示:COUNT(*)统计记录条数,AVG(age)统计年龄的平均数,SUM(age)统计年龄的总数,MAX(age)统计年龄的最大数,MIN(age)统计年龄的最小数。

可以使用WHERE子句先进行进行条件筛选再统计。

                    mysql> SELECT COUNT(*),AVG(age),SUM(age),MAX(age),MIN(age) FROM student WHERE id < 5;
                    +----------+----------+----------+----------+----------+
                    | COUNT(*) | AVG(age) | SUM(age) | MAX(age) | MIN(age) |
                    +----------+----------+----------+----------+----------+
                    |        4 |   8.5000 |       34 |       10 |        8 |
                    +----------+----------+----------+----------+----------+
                    1 rows in set (0.00 sec)
                

上述命令所示:当id小于5时,用COUNT(*)统计记录条数,AVG(age)统计年龄的平均数,SUM(age)统计年龄的总数,MAX(age)统计年龄的最大数, MIN(age)统计年龄的最小数。

可以使用GROUP BY 子句进行分组。

                    mysql> SELECT classId,COUNT(*),AVG(age),MAX(age),MIN(age) FROM student GROUP BY classId;
                    +---------+----------+----------+----------+----------+
                    | classId | COUNT(*) | AVG(age) | MAX(age) | MIN(age) |
                    +---------+----------+----------+----------+----------+
                    |       1 |        2 |   8.0000 |        8 |        8 |
                    |       2 |        4 |   8.7500 |       10 |        8 |
                    |       3 |        2 |   9.5000 |       10 |        9 |
                    +---------+----------+----------+----------+----------+
                    3 rows in set (0.00 sec)
                

上述命令所示:根据classId分组显示各个班级的记录数、年龄平均数、最大数、最小数。

使用WHER子句筛选后的数据再进行分组:

                    mysql> SELECT classId,COUNT(*),AVG(age),MAX(age),MIN(age) FROM student WHERE id <8 GROUP BY classId;
                    +---------+----------+----------+----------+----------+
                    | classId | COUNT(*) | AVG(age) | MAX(age) | MIN(age) |
                    +---------+----------+----------+----------+----------+
                    |       1 |        2 |   8.0000 |        8 |        8 |
                    |       2 |        3 |   8.6667 |       10 |        8 |
                    |       3 |        2 |   9.5000 |       10 |        9 |
                    +---------+----------+----------+----------+----------+
                    3 rows in set (0.00 sec)
                

在分组的结果中进行筛选,可以使用HAVING子句。

                    mysql> SELECT classId,COUNT(*),AVG(age),MAX(age),MIN(age) FROM student WHERE id <8 GROUP BY classId HAVING classId>1;
                    +---------+----------+----------+----------+----------+
                    | classId | COUNT(*) | AVG(age) | MAX(age) | MIN(age) |
                    +---------+----------+----------+----------+----------+
                    |       2 |        3 |   8.6667 |       10 |        8 |
                    |       3 |        2 |   9.5000 |       10 |        9 |
                    +---------+----------+----------+----------+----------+
                    2 rows in set (0.00 sec)