Support Having w/o Group

This commit is contained in:
Jinzhu 2021-03-30 18:28:09 +08:00
parent 73c6d3e64e
commit 33601dc72f
2 changed files with 16 additions and 0 deletions

View File

@ -39,4 +39,10 @@ func (groupBy GroupBy) MergeClause(clause *Clause) {
groupBy.Having = append(copiedHaving, groupBy.Having...)
}
clause.Expression = groupBy
if len(groupBy.Columns) == 0 {
clause.Name = ""
} else {
clause.Name = groupBy.Name()
}
}

View File

@ -96,4 +96,14 @@ func TestGroupBy(t *testing.T) {
if name != "groupby" || active != true || total != 40 {
t.Errorf("group by two columns, name %v, age %v, active: %v", name, total, active)
}
if DB.Dialector.Name() == "mysql" {
if err := DB.Model(&User{}).Select("name, age as total").Where("name LIKE ?", "groupby%").Having("total > ?", 300).Scan(&result).Error; err != nil {
t.Errorf("no error should happen, but got %v", err)
}
if result.Name != "groupby1" || result.Total != 330 {
t.Errorf("name should be groupby, total should be 660, but got %+v", result)
}
}
}