From 2bd6928400a1c9f5a289d53a5c8a1ea486a56165 Mon Sep 17 00:00:00 2001 From: Code Date: Sat, 1 Apr 2017 15:22:30 +0800 Subject: [PATCH 1/2] fix Count Debug --- scope.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scope.go b/scope.go index 29cb01b2..ff270bc0 100644 --- a/scope.go +++ b/scope.go @@ -940,7 +940,12 @@ func (scope *Scope) pluck(column string, value interface{}) *Scope { func (scope *Scope) count(value interface{}) *Scope { if query, ok := scope.Search.selects["query"]; !ok || !countingQueryRegexp.MatchString(fmt.Sprint(query)) { - scope.Search.Select("count(*)") + if len(scope.Search.group) != 0 { + scope.Search.Select("count(*) FROM ( SELECT count(*) ") + scope.Search.group += " ) AS count" + } else { + scope.Search.Select("count(*)") + } } scope.Search.ignoreOrderQuery = true scope.Err(scope.row().Scan(value)) From 238d1d8de84f1fde924afe3f9b6229a22b87af69 Mon Sep 17 00:00:00 2001 From: Code Date: Fri, 7 Apr 2017 03:54:03 +0800 Subject: [PATCH 2/2] fix count() bug --- scope.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scope.go b/scope.go index ff270bc0..d0528fb4 100644 --- a/scope.go +++ b/scope.go @@ -335,7 +335,7 @@ func (scope *Scope) CombinedConditionSql() string { whereSql = strings.TrimSuffix(strings.TrimPrefix(whereSql, "WHERE ("), ")") } return joinSql + whereSql + scope.groupSQL() + - scope.havingSQL() + scope.orderSQL() + scope.limitAndOffsetSQL() + scope.havingSQL() + scope.orderSQL() + scope.limitAndOffsetSQL() + scope.Search.countEnd } // Raw set raw sql @@ -942,7 +942,7 @@ func (scope *Scope) count(value interface{}) *Scope { if query, ok := scope.Search.selects["query"]; !ok || !countingQueryRegexp.MatchString(fmt.Sprint(query)) { if len(scope.Search.group) != 0 { scope.Search.Select("count(*) FROM ( SELECT count(*) ") - scope.Search.group += " ) AS count" + scope.Search.countEnd = " ) AS count" } else { scope.Search.Select("count(*)") }