【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
}
相关推荐
薛定谔的算法5 小时前
phoneGPT:构建专业领域的检索增强型智能问答系统
前端·数据库·后端
Databend7 小时前
Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
数据库
得物技术8 小时前
破解gh-ost变更导致MySQL表膨胀之谜|得物技术
数据库·后端·mysql
Raymond运维12 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉12 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger2 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql