您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/5/20 11:58:04发布23次查看
可以通过在 select 语句中使用 group by 子句来完成。我们可以借助 group by 子句指定列作为分组条件。由于指定的分组标准,特定列中具有相同值的行被视为单个组。这样,mysql select 语句返回的结果集就会被分组。
示例下面是一个很好的例子来理解它 -
我们有一个名为“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返回的结果集进行分组呢?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product