mysqlgroup by
时间: 2023-09-07 15:18:25 浏览: 92
GROUP BY is a clause in MySQL used to group rows based on one or more columns. It is often used in combination with aggregate functions like SUM, AVG, COUNT, etc., to perform calculations on specific groups of data rather than the entire dataset.
For example, let's say we have a table called "sales" with columns like "product_name," "category," and "quantity_sold." We want to find the total quantity sold for each category. We can use the GROUP BY clause to achieve this:
```sql
SELECT category, SUM(quantity_sold) as total_quantity
FROM sales
GROUP BY category;
```
This query will group the rows based on the "category" column and calculate the sum of "quantity_sold" for each category. The result will be a set of rows, each representing a category and its corresponding total quantity sold.
Note that when using GROUP BY, the SELECT clause can only contain columns that are either part of the GROUP BY clause or used with aggregate functions.
阅读全文