From 761b6c129cee9c2a2582d8d8b4dfe2300c9308d4 Mon Sep 17 00:00:00 2001 From: qAison <77456702@qq.com> Date: Fri, 27 May 2016 14:34:22 +0800 Subject: [PATCH] count func add support group by --- orm/db.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/orm/db.go b/orm/db.go index 314c3535..d5951f45 100644 --- a/orm/db.go +++ b/orm/db.go @@ -927,12 +927,17 @@ func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition tables.parseRelated(qs.related, qs.relDepth) where, args := tables.getCondSQL(cond, false, tz) + groupBy := tables.getGroupSQL(qs.groups) tables.getOrderSQL(qs.orders) join := tables.getJoinSQL() Q := d.ins.TableQuote() - query := fmt.Sprintf("SELECT COUNT(*) FROM %s%s%s T0 %s%s", Q, mi.table, Q, join, where) + query := fmt.Sprintf("SELECT COUNT(*) FROM %s%s%s T0 %s%s%s", Q, mi.table, Q, join, where, groupBy) + + if groupBy != "" { + query = fmt.Sprintf("SELECT COUNT(*) FROM (%s) AS T", query) + } d.ins.ReplaceMarks(&query)