【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
}
相关推荐
IT枫斗者枫哥2 小时前
MyBatis一对多分页:LIMIT 20,为什么凑不齐20个订单?
java·数据库
Y002 小时前
PostgreSQL 的锁:为什么你的 ALTER TABLE 会卡住,以及怎么查
数据库·postgresql
灰原喜欢柯南2 小时前
OceanBase 运维管理知识点——OCP 监控、诊断与变更控制
数据库·oceanbase
码农-0042 小时前
Springboot 集成 Ehcache操作数据库显示SQL语句设置
数据库·spring boot·sql
仍然.2 小时前
Redis---事务
数据库·redis
oradh2 小时前
Oracle 执行计划的阅读方法(二叉树结构来理解和阅读)
数据库·oracle·执行计划的阅读方法
SelectDB2 小时前
Doris 向量检索上线踩坑记录:七个排查现场与可直接复制的命令
大数据·数据库·数据分析
数据库小学妹2 小时前
MySQL深分页优化:LIMIT大偏移的根因分析与五种解法对比
数据库·mysql·性能优化
SelectDB2 小时前
一条大查询把集群打挂之后:Apache Doris 稳定性处置与资源隔离速查
大数据·数据库·数据分析