go gorm select uses group by and order by, and the returned data is different from the original struct type, so the returned data is useless.
the following is the struct information of a table in my database, which is the service alarm information:
type Alarm struct {
gorm.Model
EventId string
AlarmTime time.Time
Priority int
Status string
TplId int
StraId int
ExpId int
Metric string
Tags string
LeftValue string
RightValue string
Endpoint string
Step int
Note string `gorm:"size: 500"`
Pdl string
Domain string
Path string
Cluster string
}
I Group by a field in the database
func (topTime TopfiveTime) GetIncidentsfive(mark string) []Alarm{
var topFiveList []Alarm
topStartTime, _ := parseWithLocation("Asia/Shanghai", topTime.StartTime)
topEndTime, _ := parseWithLocation("Asia/Shanghai", topTime.EndTime)
db.Select(mark + ",count(*) AS mark_cnt").
Where("status = "problem" AND created_at BETWEEN ? AND ?",topStartTime, topEndTime).
Group(mark).
Order("mark_cnt DESC").
Limit(6).
Find(&topFiveList)
fmt.Println(topFiveList)
return topFiveList
}
the above function is a statement that executes select SQL. The result of execution in the database is as follows:
however, because the generated result is inconsistent with Alarm struct , the information returned is some unavailable data (useless data in the original Alarm data table).
asks the boss to explain how to return the required information correctly, as shown in the database.