Home > Database > Mysql Tutorial > body text

Use MySQL's GROUP BY function to group by the value of a column

WBOY
Release: 2023-07-26 09:36:20
Original
1432 people have browsed it

Use MySQL's GROUP BY function to group by the value of a certain column

In actual database applications, we often encounter situations where we need to group by the value of a certain column. MySQL provides a GROUP BY function to meet this requirement. This article will introduce how to use MySQL's GROUP BY function and provide some sample code.

First, let us take a look at the basic syntax of the GROUP BY function:

SELECT 列1, 列2, ... FROM 表名 GROUP BY 列名;
Copy after login

In the above statement, we need to use the SELECT statement to specify the column to be queried, and then use the GROUP BY keyword followed by Column name to group by. After executing the above statement, MySQL will group the results according to the specified grouping column and return the aggregated results of each grouping.

Next, let us demonstrate the usage of the GROUP BY function through several examples.

Example 1: Group by the value of a certain column and count the number of groups

Suppose we have a table named students, containing the following columns: student_id, name and age. Now we want to group students by age and count the number of students in each age group.

SELECT age, COUNT(*) 
FROM students 
GROUP BY age;
Copy after login

After executing the above statement, MySQL will group students according to age and return the number of students in each age group.

Example 2: Group by the value of a certain column and calculate the average of the grouping

Suppose we have a table named orders, containing the following columns: order_id, customer_id and order_amount. Now we want to group the orders by customer ID and calculate the average order amount for each customer.

SELECT customer_id, AVG(order_amount) 
FROM orders 
GROUP BY customer_id;
Copy after login

After executing the above statement, MySQL will group the orders according to customer ID and return the average order amount of each customer.

Example 3: Grouping by the value of multiple columns

In addition to grouping by the value of a single column, we can also group by the value of multiple columns. Suppose we have a table called sales with the following columns: product_id, customer_id, and sale_date. Now we want to group sales records according to product ID and customer ID, and count the sales quantity of each group.

SELECT product_id, customer_id, COUNT(*) 
FROM sales 
GROUP BY product_id, customer_id;
Copy after login

After executing the above statement, MySQL will group the sales records according to the product ID and customer ID, and return the sales quantity of each group.

Summary

This article introduces how to use MySQL's GROUP BY function to group by the value of a certain column, and provides some sample code. By mastering the usage of the GROUP BY function, we can flexibly perform grouping operations in database applications, thereby conducting data analysis and statistics more effectively. Hope this article helps you!

The above is the detailed content of Use MySQL's GROUP BY function to group by the value of a column. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!