From 56fffcb25b6e63540dcc2071ae653daed016105e Mon Sep 17 00:00:00 2001 From: Code Date: Tue, 29 Aug 2017 18:50:40 +0800 Subject: [PATCH] =?UTF-8?q?fix=20count()=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit COUNT()函数逻辑有错误,本应该是在执行任何SQL的时候,都可以返回正确的行数。而现在复杂的SQL集合无法正确获取行数。 --- scope.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scope.go b/scope.go index fda7f653..6b8ce53f 100644 --- a/scope.go +++ b/scope.go @@ -950,7 +950,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))