Fix Count with Select

This commit is contained in:
Jinzhu 2020-06-23 08:51:01 +08:00
parent c84a8fe571
commit 32bd6b3e8f
1 changed files with 10 additions and 8 deletions

View File

@ -268,16 +268,18 @@ func (db *DB) Count(count *int64) (tx *DB) {
if len(tx.Statement.Selects) == 0 {
tx.Statement.AddClause(clause.Select{Expression: clause.Expr{SQL: "count(1)"}})
} else if len(tx.Statement.Selects) == 1 && !strings.Contains(strings.ToLower(tx.Statement.Selects[0]), "count(") {
column := tx.Statement.Selects[0]
} else if !strings.Contains(strings.ToLower(tx.Statement.Selects[0]), "count(") {
expr := clause.Expr{SQL: "count(1)"}
if len(tx.Statement.Selects) == 1 {
if tx.Statement.Parse(tx.Statement.Model) == nil {
if f := tx.Statement.Schema.LookUpField(column); f != nil {
column = f.DBName
if f := tx.Statement.Schema.LookUpField(tx.Statement.Selects[0]); f != nil {
expr = clause.Expr{SQL: "COUNT(DISTINCT(?))", Vars: []interface{}{clause.Column{Name: f.DBName}}}
}
}
tx.Statement.AddClause(clause.Select{
Expression: clause.Expr{SQL: "COUNT(DISTINCT(?))", Vars: []interface{}{clause.Column{Name: column}}},
})
}
tx.Statement.AddClause(clause.Select{Expression: expr})
}
tx.Statement.Dest = count