【go】xorm分类统计及多表联查

文章目录

  • [1 分类统计](#1 分类统计)
  • [2 多表联查](#2 多表联查)

1 分类统计

  • 1 sql
sql 复制代码
SELECT grade_protection_level AS type, count(grade_protection_level) AS count
FROM `vital_7jvebmrryff3_asset`
WHERE (field_group = '应用信息')
        AND (asset_life_cycle = '正式')
        AND (status = 1)
GROUP BY  grade_protection_level
ORDER BY  count(grade_protection_level) DESC
  • 2 xorm
go 复制代码
func (*dAnalyse) FindCount(orgIds []any) ([]model.Analyse[int], error) {
	res := []model.Analyse[int]{}     // 映射结构为切片
	err := AppsWhere().Select("grade_protection_level as type, count(grade_protection_level) as count").
		In("organization_id", orgIds).   // sql中删去了此条件
		GroupBy("grade_protection_level").
		OrderBy("count(grade_protection_level) desc").
		Find(&res)
	return res, err
}

2 多表联查

  • xorm
go 复制代码
func (*dAnalyse) FindTotalOrderDesc(orgNames []string) ([]model.Analyse[int], error) {
	res := []model.Analyse[int]{}
	err := AppsWhere().Select("name as org, count(grade_protection_status) as count").
		Join("INNER", "vital_7jvebmrryff3_organization", "vital_7jvebmrryff3_asset.organization_id = vital_7jvebmrryff3_organization.id").   // 内联INNER,外联LEFT
		In("name", orgNames).
		Where("grade_protection_status = '未定级'").
		GroupBy("name").
		OrderBy("count(grade_protection_status) desc").Find(&res)
	return res, err
}
相关推荐
聆听。。花开雨落1 小时前
mybatis的typeHandler 作用
数据库·mybatis
pxzsky1 小时前
PG17数据库安装中分分词插件:pg_jieba
数据库·postgresql·pg_jieba
她说可以呀2 小时前
Redis哨兵
数据库·redis·bootstrap
霸道流氓气质2 小时前
KMS 密钥管理服务(Key Management Service)原理与实践
linux·服务器·数据库
kirs_ur2 小时前
CXL(Compute Express Link)— 内存扩展的未来
服务器·数据库·性能优化
MonolithIoT4 小时前
实战方案|设备备件无人值守仓库:连续化产线运维备件 7×24 小时数字化管控方案
运维·网络·数据库
瞬间&永恒~4 小时前
【MySQL】4-6:在同一个主机上使用 systemd 运行多个 MySQL服务器
服务器·数据库·mysql
心机之蛙qee4 小时前
Redis的主从、哨兵及集群
数据库·redis·缓存
gwf2165 小时前
磨损均衡算法(Wear Leveling)——SSD如何让每块闪存“公平退休“?
运维·数据库·人工智能·python·嵌入式硬件·算法·智能硬件
AAA@峥5 小时前
CentOS7 源码编译安装 MySQL5.7|SQL 基础操作 + 备份恢复完整实战
运维·数据库·sql·centos