GORM Playground Link
https://github.com/go-gorm/playground/pull/543
Description
HaveCountList func:
func (srv *commentService) HaveCountList(maps *Comment, where interface{}, limit, offset int) (info []*Comment, count int64, err error) {
query := DB.Model(&Comment{}).
Unscoped().
Joins("User").
Where(where).Where(maps).
Group(srv.table + ".id").
Count(&count).Offset(offset).Limit(limit).
Order(srv.table + ".id desc").
Find(&info)
if err = query.Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
panic(err)
}
return
}
return sql:
SELECT count(*) FROM `comment` LEFT JOIN `user` `User` ON `comment`.`author_id` = `User`.`id` WHERE `comment`.`id` = 10000 GROUP BY `comment`.`id`
SELECT `comment`.`id`,`comment`.`content_id`,`comment`.`author_id`,`comment`.`content`,`comment`.`parent_comment_id`,`comment`.`ip`,`comment`.`user_agent`,`comment`.`pushed_at`,
`comment`.`author_name`,`comment`.`author_avatar_url`,`comment`.`author_url`,`comment`.`blog_id`,`comment`.`status`,`comment`.`created_at`,`comment`.`updated_at`,`comment`.`deleted_at` FROM `comment` LEFT JOIN `user`
`User` ON `comment`.`author_id` = `User`.`id` WHERE `comment`.`id` = 10000 GROUP BY `comment`.`id` ORDER BY comment.id desc
NormalList func :
func (srv *commentService) NormalList(maps *Comment, where interface{}, limit, offset int) (info []*Comment, count int64, err error) {
query := DB.Model(&Comment{}).
Unscoped().
Joins("User").
Where(where).Where(maps).
Group(srv.table + ".id").
Offset(offset).Limit(limit).
Order(srv.table + ".id desc").
Find(&info)
if err = query.Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
panic(err)
}
return
}
return sql:
SELECT `comment`.`id`,`comment`.`content_id`,`comment`.`author_id`,`comment`.`content`,`comment`.`parent_comment_id`,`comment`.`ip`,`comment`.`user_agent`,`comment`.`pushed_at`,
`comment`.`author_name`,`comment`.`author_avatar_url`,`comment`.`author_url`,`comment`.`blog_id`,`comment`.`status`,`comment`.`created_at`,`comment`.`updated_at`,`comment`.`deleted_at`,
`User`.`id` AS `User__id`,`User`.`name` AS `User__name`,`User`.`channel_id` AS `User__channel_id`,`User`.`sign_time` AS `User__sign_time` FROM `comment` LEFT JOIN `user`
`User` ON `comment`.`author_id` = `User`.`id` WHERE `comment`.`id` = 10000 GROUP BY `comment`.`id` ORDER BY comment.id desc
Comment From: a631807682
https://gorm.io/docs/method_chaining.html#New-Session-Method