【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
}
相关推荐
神龙斗士24014 小时前
增删改查操作
数据库·mysql
Elastic 中国社区官方博客15 小时前
13.7万人,零人工决策:使用 Elasticsearch 实现智能体驱动的灾害响应系统
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
yuzhiboyouye15 小时前
sql增删改查怎么写?有时会不会有联表查询的增删查改
数据库·sql
jingyu飞鸟15 小时前
openEuler 22.03 LTS SP4安装华为opengauss 22.03 LTS版本数据库,一键复制安装使用,保姆级教程
数据库·华为
IvorySQL15 小时前
【HOW 2026 分论坛演讲】PG/IvorySQL私有云中实践
数据库·人工智能·sql·postgresql
SAP庖丁解码15 小时前
【采购申请的校验——成本中心范围】
数据库
雪的季节16 小时前
HTTP 和 HTTPS 五大核心区别
数据库·http·https
GottdesKrieges16 小时前
OceanBase迁移用户及其权限配置
数据库·oceanbase
OceanBase数据库官方博客16 小时前
新版本 OceanBase seekdb 1.3.0:22倍性能增益,P99抖动小于1.1倍
数据库·oceanbase
倒流时光三十年16 小时前
PostgreSQL ON CONFLICT DO UPDATE 增加 WHERE 条件优化性能
数据库·postgresql