forked from mirror/gorm
Support Having w/o Group
This commit is contained in:
parent
73c6d3e64e
commit
33601dc72f
|
@ -39,4 +39,10 @@ func (groupBy GroupBy) MergeClause(clause *Clause) {
|
||||||
groupBy.Having = append(copiedHaving, groupBy.Having...)
|
groupBy.Having = append(copiedHaving, groupBy.Having...)
|
||||||
}
|
}
|
||||||
clause.Expression = groupBy
|
clause.Expression = groupBy
|
||||||
|
|
||||||
|
if len(groupBy.Columns) == 0 {
|
||||||
|
clause.Name = ""
|
||||||
|
} else {
|
||||||
|
clause.Name = groupBy.Name()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,4 +96,14 @@ func TestGroupBy(t *testing.T) {
|
||||||
if name != "groupby" || active != true || total != 40 {
|
if name != "groupby" || active != true || total != 40 {
|
||||||
t.Errorf("group by two columns, name %v, age %v, active: %v", name, total, active)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue