Tags
MySQL , Sql
Asked 2 years ago
20 Apr 2022
Views 416
shyam

shyam posted

MySQL - How to GROUP BY multiple columns ?

how to apply group by multiple columns in a select query?

select * from orders group by restaurant_id 


I have table orders where restaurants place orders for recipes, so I want to know the count of orders or specific recipes for each restaurant.
ajamil

ajamil
answered Jun 25 '23 00:00

In MySQL , you can use the GROUP BY clause to group rows based on one or more columns. To group by multiple columns, you simply list those columns separated by commas within the GROUP BY clause.

Here's an example of how to use GROUP BY with multiple columns:



SELECT column1, column2, COUNT(*)
FROM table_name
GROUP BY column1, column2;

In the example above, column1 and column2 are the columns you want to group by. The COUNT(*) function is used to count the number of rows in each group. You can replace column1 and column2 with the actual column names from your table, and table_name with the name of your table.

By specifying multiple columns in the GROUP BY clause, the result set will be grouped based on unique combinations of values from those columns. The COUNT(*) function will then provide the count of rows in each group.
Post Answer