mysql group by count
时间: 2023-08-22 10:09:52 浏览: 103
To use `GROUP BY` with `COUNT` in MySQL, you can count the number of occurrences of a specific column value in each group. Here's an example:
Suppose you have a table called `orders` with columns `product_id` and `quantity`, and you want to count the number of orders for each product. You can use `GROUP BY` along with the `COUNT` function as follows:
```sql
SELECT product_id, COUNT(*) as order_count
FROM orders
GROUP BY product_id;
```
In this example, the `COUNT(*)` function is used to count the number of rows in each group. The result will be a set of rows where each row represents a unique `product_id`, along with the corresponding count of orders (`order_count`) for that product.
You can also add additional columns in the `SELECT` clause if you want to retrieve more information about each product or perform calculations on other columns.
阅读全文