【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
}
相关推荐
得物技术3 小时前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug7 小时前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom7 小时前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*7 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
Slaughter信仰7 小时前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第十章知识点问答(10题)
java·jvm·数据库
麦兜*7 小时前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
-Xie-8 小时前
Mysql杂志(十六)——缓存池
数据库·mysql·缓存
七夜zippoe8 小时前
缓存与数据库一致性实战手册:从故障修复到架构演进
数据库·缓存·架构