mysql grouping 函数
时间: 2023-09-09 16:08:21 浏览: 114
MySQL provides several grouping functions that can be used to perform aggregate calculations on groups of rows in a table. These functions are used with the GROUP BY clause to group the rows based on one or more columns.
Here are some of the commonly used MySQL grouping functions:
1. COUNT: It returns the number of rows that match a given condition.
2. SUM: It returns the sum of the values in a column.
3. AVG: It returns the average of the values in a column.
4. MAX: It returns the maximum value in a column.
5. MIN: It returns the minimum value in a column.
6. GROUP_CONCAT: It concatenates the values of a column within each group into a single string.
To use these functions, you need to specify the column(s) that you want to group by in the GROUP BY clause, and then use the desired grouping function in the SELECT statement. For example, the following query calculates the total sales for each product category:
```
SELECT category, SUM(sales) as total_sales
FROM sales_table
GROUP BY category;
```
In this query, the SUM function is used to calculate the total sales for each category, and the GROUP BY clause is used to group the rows by category.
阅读全文