最近工作中遇到了需要使用where in (?)的情況,一直嘗試了幾個都不行,最近在官方找到了一個解決辦法,話不多說Talk is cheap,Show me the code。
...
IdInt64List = []int64{ 1, 2, 3, 5} // 需要放在where in裏面更新的IdList
db := config.DB // 數據庫連接
query, args, err := sqlx.In("UPDATE TestTable set model = ? , `status` = 1 WHERE id in (?)", Model, IdInt64List)
if err != nil {
return err
}
query = db.Rebind(query) // Rebind query
res, err := db.Exec(query, args ...)
if err != nil {
return err
}
...