当前位置:网站首页>Assembly method of golang Gorm query arbitrary fields
Assembly method of golang Gorm query arbitrary fields
2022-07-28 03:29:00 【User nickname cannot be empty】
Query the summary method of the specified unknown length field
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 Realize the specified query of any field
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", // Combine
}
err = SumFields(&filter, results, fields)
if err != nil {
log.Fatal(err)
}
fmt.Println("results = ", results)
}
It is applicable to the use of summary tables
边栏推荐
- bp svm的缺陷检测 树叶缺陷 叶片缺陷检测的系统设计
- The digital twin smart building visualization platform realizes the integration of enterprise and public services in the park
- golang 获取循环嵌套结构的tag
- MySQL的碎片有哪些
- D2DEngine食用教程(4)———绘制文本
- 【类的本质(Objective-C语言中)】
- Hotel VR panoramic display shooting provides more opportunities for cooperation and negotiation
- QT official example: Fridge Magnets example
- QFileDevice、QFile、QSaveFile、QTemporaryFile
- How to arrange PCB screen printing? Please check this manual!
猜你喜欢

redis源码分析(谁说C语言就不能分析了?)

The open source of "avoiding disease and avoiding medicine" will not go far

响应式高端网站模板源码图库素材资源下载平台源码

20220727 use the Bluetooth module hc-05 of Huicheng technology to pair mobile phones for Bluetooth serial port demonstration

Softek Barcode Reader 9.1.5

ssm整合(整合配置)

20220726汇承科技的蓝牙模块HC-05的AT命令测试

Redis持久化机制

如何卸载干净zabbix服务?(超详细)

What are the fragments of MySQL
随机推荐
The digital twin smart building visualization platform realizes the integration of enterprise and public services in the park
redis源码分析(谁说C语言就不能分析了?)
SSM integration (integrated configuration)
Redis基本操作
Shell writing specifications and variables
Shell: resource monitoring script and high load alarm
bp svm的缺陷检测 树叶缺陷 叶片缺陷检测的系统设计
Redis实现分布式锁
如何一键进行重装Win11系统
How to solve the problem of win11 black desktop background?
Leetcode 29th day
C -- switch case statement
【Codeforces Round #806 (Div. 4)(A~F)】
Color recognition method and exploration based on MATLAB
Redis memory recycling
20220726 at command test of Bluetooth module hc-05 of Huicheng Technology
GNU General Public License v2.0 GNU General Public License
IronOCR for .NET 2022.8
Practice of online problem feedback module (16): realize the function of checking details
工程地质实习-工程地质 题集