0

The following statement is used:

id []int64

type Score struct {
    Score int `gorm:"score"`
}
func GetScoreByID(score *Score, id []int64) error {
    db.Table("question_bank").Select("sum(score) as score").Where("id IN ?", id).Scan(&score).Error
}

Report the following error:

Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?)' at line 1

How can I write to achieve this functionality?

YuSheng
  • 97
  • 7
  • 1
    Try read [gorm offical docs](https://gorm.io/docs/advanced_query.html#Count) – NicoXiang Mar 30 '22 at 10:01
  • Does this answer your question? [How to get sum of salary column from table GORM](https://stackoverflow.com/questions/53757955/how-to-get-sum-of-salary-column-from-table-gorm) – NicoXiang Mar 30 '22 at 10:02

1 Answers1

0

Just change function parameter score *Score to score *int64 and as score is pointer so no need to put & before score in Scan.

func GetScoreByID(score *int64, id []int64) error {
    return db.Table("question_bank").
      Select("sum(score)").
      Where("id IN ?", id).
      Scan(score).Error
}
Shahriar Ahmed
  • 502
  • 4
  • 11