在GORM中使用SQL的IIF函数是否可行?

huangapple go评论115阅读模式
英文:

Is it possible to use SQL IIF function in GORM?

问题

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

我尝试了这个:

  1. import (
  2. "fmt"
  3. "gorm.io/gorm"
  4. "encoding/json"
  5. "github.com/my_organisation/myorg-repo/db"
  6. )
  7. var database *gorm.DB
  8. var solutions []db.Solution
  9. var listOfAsnmtIDs []uint
  10. func myfuncn (w http.ResponseWriter, r *http.Request){
  11. //...
  12. _ = database.Table("solutions").Where("pupil_id = ?",
  13. pupil.ID).Select("assignment_id").Find(&solutions)
  14. for _, solution := range solutions {
  15. listOfAsnmtIDs = append(listOfAsnmtIDs, solution.AssignmentID)
  16. }
  17. response := database.Table("assignments").Select(`id, created_at, IIF((id IN ?), 'attempted', 'Not attempted') as attempted`, listOfAsnmtIDs).Find(&allAssignments)
  18. if response.RowsAffected < 1 {
  19. respondToClient(w, 404, nil, "No assignments found")
  20. return
  21. }
  22. //...
  23. }

请注意,我只翻译了代码部分,其他内容不包括在内。

英文:

I have a a table for assignments, one for solutions and another for students.
I want to retrieve all assignments and against each I want to add a 'flag' showing that the currently logged-in student has attempted the assignment or not.

I have tried this:

  1. import (
  2. &quot;fmt&quot;
  3. &quot;gorm.io/gorm&quot;
  4. &quot;encoding/json&quot;
  5. &quot;github.com/my_organisation/myorg-repo/db&quot;
  6. )
  7. var database *gorm.DB
  8. var solutions []db.Solution
  9. var listOfAsnmtIDs []uint
  10. func myfuncn (w http.ResponseWriter, r *http.Request){
  11. //...
  12. _ = database.Table(&quot;solutions&quot;).Where(&quot;pupil_id = ?&quot;,
  13. pupil.ID).Select(&quot;assignment_id&quot;).Find(&amp;solutions)
  14. for _, solution := range solutions {
  15. listOfAsnmtIDs = append(listOfAsnmtIDs, solution.AssignmentID)
  16. }
  17. response := database.Table(&quot;assignments&quot;).Select(`id, created_at, IIF((id IN ?), &#39;attempted&#39;, &#39;Not attempted&#39;) as attempted`, listOfAsnmtIDs).Find(&amp;allAssignments)
  18. if response.RowsAffected &lt; 1 {
  19. respondToClient(w, 404, nil, &quot;No assignments found&quot;)
  20. return
  21. }
  22. //...
  23. }

答案1

得分: 0

你只需要列出参数。像这样:

  1. var mad string
  2. for i, solution := range solutions {
  3. mad += strconv.FormatUint(uint64(solution.AssignmentID), 10)
  4. if i != len(solutions) {
  5. mad += ","
  6. }
  7. listOfAsnmtIDs = append(listOfAsnmtIDs, solution.AssignmentID)
  8. }
  9. response := database.Table("assignments").Select(`id, created_at, IIF((id IN ?), 'attempted', 'Not attempted') as attempted`, mad).Find(&allAssignments)
英文:

You just need to list params. Something like this

  1. var mad string
  2. for i, solution := range solutions {
  3. mad += strconv.FormatUint(uint64(solution.AssignmentID), 10)
  4. if i != len(solutions) {
  5. mad += &quot;,&quot;
  6. }
  7. listOfAsnmtIDs = append(listOfAsnmtIDs, solution.AssignmentID)
  8. }
  9. response := database.Table(&quot;assignments&quot;).Select(`id, created_at, IIF((id IN ?), &#39;attempted&#39;, &#39;Not attempted&#39;) as attempted`, mad).Find(&amp;allAssignments)

huangapple
  • 本文由 发表于 2022年2月2日 21:52:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/70956843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定