Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to write group by for multiple fields #7245

Closed
wlbwlbwlb opened this issue Oct 17, 2024 · 3 comments
Closed

How to write group by for multiple fields #7245

wlbwlbwlb opened this issue Oct 17, 2024 · 3 comments
Assignees
Labels
type:question general questions

Comments

@wlbwlbwlb
Copy link

wlbwlbwlb commented Oct 17, 2024

Your Question

The document you expected this should be explained

like this?
db.Group("age,score")

Expected answer

@wlbwlbwlb wlbwlbwlb added the type:question general questions label Oct 17, 2024
@alex-guoba
Copy link

Group belongs to Chaining Method.

db.Group("age").Group("score")

@sandeepchowdary7
Copy link

Hello @wlbwlbwlb

GORM's Group method allows grouping by multiple fields by passing a comma-separated string of field names. Here's an example:

var results []YourModel
db.Model(&YourModel{}).
    Select("field1, field2, COUNT(*) as count").
    Group("field1, field2").
    Find(&results)

In this query, field1 and field2 are the fields you want to group by, and count will hold the count of records for each group.

Cheers!!

@wlbwlbwlb
Copy link
Author

Resolved. Thank you all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

4 participants