当前位置:网站首页>golang gorm查询任意字段的组装方法
golang gorm查询任意字段的组装方法
2022-07-28 02:58:00 【用户昵称不能为空】
查询指定未知长度字段的汇总方法
package main
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"log"
"strings"
)
type FilterDo struct {
}
type ScanFields interface {
IsScanField() bool
}
type Summary struct {
Num1 int `json:"num1"` // num1
Num2 int `json:"num2"` // num2
Num3 int `json:"num3"` // num3
Num4 int `json:"num4"` // num4
}
func (Summary) IsScanField() bool {
return true
}
var db *gorm.DB
func GetDB() *gorm.DB {
return db
}
type TableMember struct {
}
func ListCond(filter *FilterDo) (whereSql string, params []interface{
}) {
return
}
// SumFields gorm 实现指定查询任意字段
func SumFields(filter *FilterDo, results ScanFields, fields map[string]string) (err error) {
if len(fields) == 0 {
return
}
alias := make([]string, 0, len(fields))
names := make([]string, 0, len(fields)+1)
names = append(names, "m.id")
for k, v := range fields {
alias = append(alias, fmt.Sprintf("sum(%s) %s", strings.ReplaceAll(k, ",", "+"), v))
if v != "id" {
names = append(names, fmt.Sprintf("m.%s", strings.ReplaceAll(k, ",", ",m.")))
}
}
condition, params := ListCond(filter)
subQuery := GetDB().Model(&TableMember{
}).
Select("distinct "+strings.Join(names, ",")).
Table("table_member m").
Joins("left join a2 on m.user_id = a2.id ").
Joins("left join a3 on m.user_id = a3.user_id ").
Unscoped().
Where(condition, params...)
err = GetDB().Raw("select "+strings.Join(alias, ",")+" from ? a ", subQuery.SubQuery()).Scan(results).Error
return
}
func main() {
var (
err error
filter FilterDo
results Summary
)
fields := map[string]string{
"today_num": "num1",
"yes_num": "num2",
"last_num": "num3",
"today_num+last_num": "num4", // 组合
}
err = SumFields(&filter, results, fields)
if err != nil {
log.Fatal(err)
}
fmt.Println("results = ", results)
}
适用于使用汇总统计表的时候
边栏推荐
猜你喜欢

Practice of online problem feedback module (16): realize the function of checking details

如何解决mysql深分页问题

【下载文件】uniapp开发小程序,下载文件并保存到本地

Analysis of redis network model

What if the word selection box of win11 input method is missing?

Shell:一键部署pxe

How to reinstall win11 system with one click

《MySQL数据库进阶实战》读后感(SQL 小虚竹)

MySQL事务的ACID特性及并发问题实例分析

Scheme sharing | experts gather to jointly explore accent AI speech recognition
随机推荐
Practice of online problem feedback module (16): realize the function of checking details
【ACwing 1064 小国王】状压dp
Redis内存回收
When QML uses layout layout, a large number of < unknown file >: QML qquicklayoutattached: binding loop detected for property circular binding warnings appear
STM32之IO模拟串口篇
决策树与随机森林学习笔记(1)
[acwing 327. corn field] shaped pressure DP
IronOCR for .NET 2022.8
C#WinForm开发:如何将图片添加到项目资源文件(Resources)中
嵌入式数据库--SQLite
Original title of Blue Bridge Cup
Comprehensive comparative study of image denoising
Alibaba cloud international email service package purchase process
CAD creation group is not combined?
【2022 牛客第二场J题 Link with Arithmetic Progression】三分套三分/三分极值/线性方程拟合最小二乘法
How to reinstall win11 system with one click
【Codeforces Round #806 (Div. 4)(A~F)】
Redis implements distributed locks
Win11黑色桌面背景如何解决?
Uniapp——拨打电话、发送短信