搜索

首页  >  问答  >  正文

GORM是否支持使用SQL的IIF函数?

<p>我有一个用于作业的表,一个用于解决方案的表,还有一个用于学生的表。 我想检索所有的作业,并且对于每个作业,我想添加一个“标志”,显示当前登录的学生是否尝试过该作业。</p> <p>我尝试了这个:</p> <pre class="brush:golang;toolbar:false;">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 } //... } </pre></p>
P粉547362845P粉547362845470 天前600

全部回复(1)我来回复

  • P粉718730956

    P粉7187309562023-09-02 09:29:54

    您只需要列出参数。类似于这样

        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)
    

    回复
    0
  • 取消回复