When working with datasets in MySQL, it is often necessary to group data based on multiple columns to identify patterns and summarize information. In a MySQL SELECT query, the GROUP BY clause allows users to group rows by one or more columns.
Can we GROUP BY Multiple Columns?
Yes, it is possible to group by multiple columns in a MySQL SELECT query. By specifying several columns after the GROUP BY keyword, you can group the result rows by the intersection of values in those columns.
Syntax:
The syntax to group by multiple columns is as follows:
SELECT aggregate_function(column_name) FROM table_name GROUP BY col1, col2, col3, ...
Where:
Example:
Consider the following query that groups rows by both the tier_id and form_template_id columns:
SELECT COUNT(*) AS count FROM fV GROUP BY fV.tier_id, f.form_template_id;
This query will count the number of rows for each unique combination of tier_id and form_template_id in the fV table. The result will be a table with two columns: tier_id, form_template_id, and count.
The above is the detailed content of How to GROUP BY Multiple Columns in MySQL?. For more information, please follow other related articles on the PHP Chinese website!