我有一张作业表,一张用于解决方案,另一张用于学生。我想检索所有作业,并针对每个作业添加一个“标志”,表明当前登录的学生是否尝试过作业。
我试过这个:
import (
"fmt"
"gorm.io/gorm"
"encoding/json"
"github.com/my_organisation/myorg-repo/db"
)
var database *gorm.DB
var solutions []db.Solution
var listOfAsnmtIDs []uint
func myfuncn (w http.ResponseWriter, r *http.Request){
//...
_ = database.Table("solutions").Where("pupil_id = ?",
pupil.ID).Select("assignment_id").Find(&solutions)
for _, solution := range solutions {
listOfAsnmtIDs = append(listOfAsnmtIDs, solution.AssignmentID)
}
response := database.Table("assignments").Select(`id, created_at, IIF((id IN ?), 'attempted', 'Not attempted') as attempted`, listOfAsnmtIDs).Find(&allAssignments)
if response.RowsAffected < 1 {
respondToClient(w, 404, nil, "No assignments found")
return
}
//...
}