Home  >  Article  >  Database  >  How do we apply filtering conditions at the group level of the result set returned by MySQL?

How do we apply filtering conditions at the group level of the result set returned by MySQL?

王林
王林forward
2023-09-17 21:45:03969browse

我们如何在 MySQL 返回的结果集的组级别应用过滤条件?

As we all know, the GROUP BY clause in the SELECT statement can group the result set returned by MySQL. Now, if we only want to return some specific groups, we need to apply the filter at the group level. This can be done by using the HAVING clause in the GROUP BY clause. The following example will demonstrate it -

Example

Suppose we only want to return the group with an average salary of 55000, then we need to use the following filter conditions in the HAVING clause-

mysql> Select count(*),AVG(salary),Designation from employees GROUP BY designation having AVG(salary) = 55000;

+----------+-------------+-------------+
| count(*) | AVG(salary) | Designation |
+----------+-------------+-------------+
| 2        | 55000.0000  | Asst.Prof   |
+----------+-------------+-------------+

1 row in set (0.00 sec)

The above is the detailed content of How do we apply filtering conditions at the group level of the result set returned by MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete