我想在一个查询中更新多行:
//threadIDs is a variable length slice of integers like [3 5 6]
query := "UPDATE message SET recipient_deleted=? WHERE recipient_id=? AND thread_id IN ?"
_, err := database.SQL.Exec( query, 1, userID, threadIDs)
if err != nil {
log.Fatal(err)
}
但是我遇到了运行时错误:
sql: converting argument $3 type: unsupported type []int, a slice of int
我该如何解决?
答案 0 :(得分:1)
您可以使用sqlx.In
来返回一个新查询和一个新的args切片,然后使用它们代替原始值,如下所示:
query := "UPDATE message SET recipient_deleted=? WHERE recipient_id=? AND thread_id IN (?)"
qry, args, err := sqlx.In(query, 1, userID, threadIDs)
if err != nil {
panic(err)
}
if _, err := database.SQL.Exec(qry, args...); err != nil {
panic(err)
}