From 32bd6b3e8f126e1d52e8ebb31b7533389b875ae0 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 23 Jun 2020 08:51:01 +0800 Subject: [PATCH] Fix Count with Select --- finisher_api.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/finisher_api.go b/finisher_api.go index 92d4fe72..b443f4b5 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -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] - if tx.Statement.Parse(tx.Statement.Model) == nil { - if f := tx.Statement.Schema.LookUpField(column); f != nil { - column = f.DBName + } 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(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