1 回答
TA贡献1796条经验 获得超4个赞
要回答这个问题。提供的上下文还不够。但我会添加一些答案。
您可以迭代“listOf”映射并为每个项目构建单独的查询并在数据库中执行。或者迭代map并追加“itemIds”分别查询comman(,)在db中执行。
例 1:
//Your Way
var listOf []map[string]interface{}
listOf = append(listOf, map[string]interface{}{})
listOf[0]["1"] = "1"
listOf[0]["3"] = "3"
query2 := "select * from sometable where id= %s"
ids := ""
for _, v := range listOf {
for _, v1 := range v {
// replace logic
if ids == "" {
ids = fmt.Sprintf(`%v`, v1)
continue
}
ids += fmt.Sprintf(`,%v`, v1)
}
}
query2 = fmt.Sprintf(query2, ids) // select * from sometable where id= 3,1
fmt.Println(query2)
例 2:
// Easy Way
listOf := make(map[string]interface{})
listOf["1"] = "1"
listOf["2"] = "2"
query2 := "select * from sometable where id= %s"
ids := ""
for _, v := range listOf {
// replace logic
if ids == "" {
ids = fmt.Sprintf(`%v`, v)
continue
}
ids += fmt.Sprintf(`,%v`, v)
}
query2 = fmt.Sprintf(query2, ids) // select * from sometable where id= 1,2
fmt.Println(query2)
- 1 回答
- 0 关注
- 81 浏览
添加回答
举报