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

数据库数据库 2023-08-31 03:09:48 749
摘要: 示例下面是一个很好的例子来理解它-我们有一个名为“employees”的表,如下-mysql>Select*fromemployees;+------+-------------+--------+------------+|id|designation|Salary|DoJ|+-----...

如何对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返回的结果集进行分组呢?的详细内容,更多请关注其它相关文章!