GROUP BY usage in MySQL allows grouping data and calculating aggregate values. The syntax is: Specify the grouping column: GROUP BY group_column_name Apply the aggregate function: aggregate_function(column_name) Return the grouping and aggregation results from the table: SELECT ... FROM table_name
GROUP BY usage in MySQL
As an important keyword for data aggregation in MySQL, GROUP BY allows us to group data in query results and perform further analysis based on the grouping results. calculate.
Syntax
<code class="sql">SELECT aggregate_function(column_name) FROM table_name GROUP BY group_column_name;</code>
Parameters
Usage
Example
<code class="sql">SELECT department_id, SUM(salary) AS total_salary FROM employee GROUP BY department_id;</code>
This query groups employee data by department and calculates the total salary for each department. The results are as follows:
department_id | total_salary |
---|---|
10000 | |
15000 | |
20000 |
Notes
The above is the detailed content of How to use group by in mysql. For more information, please follow other related articles on the PHP Chinese website!