文章目录
- [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
}