From 51c8eb21a2da551237b343b4c45bbbbba3c00f26 Mon Sep 17 00:00:00 2001 From: lingxin <961347548@qq.com> Date: Mon, 17 Mar 2025 09:49:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96count=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lxDb/sql.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/lxDb/sql.go b/lxDb/sql.go index 8c44100..6b4cf11 100644 --- a/lxDb/sql.go +++ b/lxDb/sql.go @@ -241,7 +241,12 @@ func SqlQuery(tx *gorm.DB, sql string, list interface{}, q *PaginationQuery, par if needDoCount(q) { var total int64 //tx = tx.Count(&total) - tx.Raw("SELECT COUNT(*) as total FROM ("+sql2+") aaaa", params...).Take(&total) + //tx.Raw("SELECT COUNT(*) as total FROM ("+sql2+") aaaa", params...).Take(&total) + //todo lcs 优化速度 start + // 替换 SELECT 和 FROM 之间的部分,并去掉 GROUP BY + replacedSQL := replaceSelectAndRemoveGroupBy(sql2) + tx.Raw(replacedSQL, params...).Take(&total) + //todo end q.Total = int(total) if total == 0 { // 如果查了记录条数并且是0, 就不需要查记录和汇总了 return @@ -396,3 +401,34 @@ func Transaction(txItem, txMain *gorm.DB, fun func(txItem, txMain *gorm.DB) (err } return } + +// replaceSelectAndRemoveGroupBy 替换 SELECT 和 FROM 之间的部分,并去掉 GROUP BY +func replaceSelectAndRemoveGroupBy(sql string) string { + // 找到 SELECT 和 FROM 的位置 + selectIndex := strings.Index(strings.ToUpper(sql), "SELECT") + fromIndex := strings.Index(strings.ToUpper(sql), "FROM") + + if selectIndex == -1 || fromIndex == -1 { + return sql // 如果没有找到 SELECT 或 FROM,返回原始 SQL + } + + // 替换 SELECT 和 FROM 之间的部分 + newSelectClause := "COUNT(DISTINCT ***) AS total" + replacedSQL := sql[:selectIndex+6] + " " + newSelectClause + " " + sql[fromIndex:] + + // 去掉 GROUP BY 子句 + groupByIndex := strings.Index(strings.ToUpper(replacedSQL), "GROUP BY") + if groupByIndex != -1 { + c := replacedSQL[groupByIndex+8:] + c = strings.TrimSpace(c) + c = strings.ReplaceAll(c, ";", "") + c = strings.Split(c, ",")[0] + replacedSQL = replacedSQL[:groupByIndex] + replacedSQL = strings.ReplaceAll(replacedSQL, "DISTINCT ***", "DISTINCT "+c) + } else { + //没有group by 的 + replacedSQL = strings.ReplaceAll(replacedSQL, "DISTINCT ***", "*") + } + + return replacedSQL +}