如何对MySQL返回的结果集进行分组呢?

王林
王林 转载
2023-08-27 17:17:02 625浏览

如何对MySQL返回的结果集进行分组呢?

示例

下面是一个很好的例子来理解它 -

我们有一个名为“employees”的表,如下 -

mysql> Select * from employees;
+------+-------------+--------+------------+
| id   | designation | Salary | DoJ        |
+------+-------------+--------+------------+
| 100  | Asst.Prof   | 50000  | 2016-06-15 |
| 300  | Prof        | 85000  | 2010-05-18 |
| 250  | Asso.Prof   | 74000  | 2013-02-12 |
| 400  | Prof        | 90000  | 2009-05-19 |
| 200  | Asst.Prof   | 60000  | 2015-05-11 |
+------+-------------+--------+------------+
5 rows in set (0.00 sec)

现在在以下脚本的帮助下,我们将输出分组;

mysql> select designation, count(*), AVG(salary) from employees group by designation;
+-------------+----------+-------------+
| designation | count(*) | AVG(salary) |
+-------------+----------+-------------+
| Asso.Prof   | 1        | 74000.0000  |
| Asst.Prof   | 2        | 55000.0000  |
| Prof        | 2        | 87500.0000  |
+-------------+----------+-------------+
3 rows in set (0.00 sec)

以上查询返回的结果集为 Asso.Prof,总共 1 个,平均工资为 74000,Asst.Prof,总共 2 个,平均工资为 55000,Prof,总共 2 个,平均工资为 74000。平均工资87500。

以上就是如何对MySQL返回的结果集进行分组呢?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除