Home > Article > Daily Programming > Statements used to group in mysql
The statement used for grouping in MySQL is GROUP BY. It summarizes data by grouping by specified columns and performing aggregate functions (such as SUM, COUNT, AVG) on each group.
Group statement in MySQL: GROUP BY
Question: Used for grouping in MySQL What is the statement of?
Answer: GROUP BY
Detailed description:
The GROUP BY statement is used to group data by specified columns, and then Perform aggregate functions (such as SUM, COUNT, AVG) for each group.
Syntax:
<code class="sql">SELECT aggregate_function(column_name) FROM table_name GROUP BY column_name;</code>
Where:
Usage:
The GROUP BY statement can be used with multiple aggregate functions, for example:
<code class="sql">SELECT SUM(sales), COUNT(*) FROM orders GROUP BY product_id;</code>
The query will follow ## The #product_id column sums the
sales column and calculates the quantity sold for each product.
Example:
The following example queries sales data, groups by product type and calculates the total sales of each group:<code class="sql">SELECT product_type, SUM(sales) AS total_sales FROM sales_data GROUP BY product_type;</code>The output may be as follows :
total_sales | |
---|---|
10000 | |
5000 | |
2000 |
The above is the detailed content of Statements used to group in mysql. For more information, please follow other related articles on the PHP Chinese website!