1

我有一张作业表,一张用于解决方案,另一张用于学生。我想检索所有作业,并针对每个作业添加一个“标志”,表明当前登录的学生是否尝试过作业。

我试过这个:

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
 } 
//...
}
4

1 回答 1

0

你只需要列出参数。像这样的东西

    var mad string
    for i, solution := range solutions {
        mad += strconv.FormatUint(uint64(solution.AssignmentID), 10)
        if i != len(solutions) {
            mad += ","
        }
        listOfAsnmtIDs = append(listOfAsnmtIDs, solution.AssignmentID)
    }

    response := database.Table("assignments").Select(`id, created_at, IIF((id IN ?), 'attempted', 'Not attempted') as attempted`, mad).Find(&allAssignments)
于 2022-02-02T16:56:03.403 回答