Home > Daily Programming > Mysql Knowledge > What does having in mysql do?

What does having in mysql do?

下次还敢
Release: 2024-04-27 03:00:24
Original
1124 people have browsed it

MySQL's HAVING keyword is used after GROUP BY to filter the grouped results and select only rows that meet the specified conditions. Syntax: SELECT FROM GROUP BY HAVING It allows applying conditions to grouped columns, aggregate function results, or expressions.

What does having in mysql do?

The use of the HAVING keyword in MySQL

The HAVING keyword is used in MySQL in the GROUP BY subsection statement to apply the condition on the grouped data. It allows filtering of grouped results to select only rows that meet specified criteria.

Syntax

<code class="sql">SELECT <column_list>
FROM <table_name>
GROUP BY <group_by_column>
HAVING <condition></code>
Copy after login

How to use

  1. Specify grouping columns:Use GROUP BY clause specifies the columns to be grouped.
  2. Add HAVING condition: After the GROUP BY clause, use HAVING to specify a condition that is used to filter the grouped results.
  3. Apply conditions: HAVING conditions can be applied to grouped columns, the results of aggregate functions, or other expressions.

Example

For example, to find salespeople with sales of more than $1,000 from the "sales" table, you would use the following query:

<code class="sql">SELECT salesperson_name, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY salesperson_name
HAVING total_sales > 1000;</code>
Copy after login

In this query:

  • GROUP BY salesperson_name: Group salespersons by name.
  • HAVING total_sales > 1000: Select only salespeople with sales over $1000.

Note:

  • HAVING conditions can only refer to grouped columns and aggregate functions.
  • HAVING clause is used after GROUP BY clause.
  • The HAVING keyword is used to further filter the grouped results, while the WHERE keyword is used to filter the underlying data before grouping.

The above is the detailed content of What does having in mysql do?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template