The grouping function in MySQL is used to calculate aggregate values by grouping the data set. Commonly used functions are: SUM: Calculate the sum of the values in the specified column COUNT: Calculate the number of non-NULL values in the specified column AVG: Calculate the average value of the values in the specified column MIN: Calculate the minimum value in the specified column MAX: Calculate the number of non-NULL values in the specified column The maximum value of
MySQL grouping function
The grouping function in MySQL is used to group based on one or more groups of columns Group the dataset and calculate aggregate values (e.g. SUM, COUNT, AVG) for each group. The following is the syntax of commonly used grouping functions:
<code class="sql">SELECT 列1, 列2, 聚合函数(列3) FROM 表名 GROUP BY 列1, 列2</code>
Commonly used grouping functions
Group function usage example
The following example demonstrates how to use the group function to calculate the total sales of each product in the sales record:
<code class="sql">SELECT product_id, SUM(quantity_sold) AS total_sales FROM sales_records GROUP BY product_id;</code>
The results will show the product_id and total sales of each product.
Note
The above is the detailed content of How to write grouping function in mysql. For more information, please follow other related articles on the PHP Chinese website!